Import serializer in models.py : Django rest framwork
<p>My one model class having following models,</p>

<p><strong>NewsfeedModel.py</strong></p>

<pre><code>class NewsFeed(models.Model):

class NewsStatus(models.Model):

class NewsImage(models.Model):
</code></pre>

<p>this is my serializers.py file</p>

<pre><code>from MadhaparGamApps.AppModels.NewsfeedModel import NewsFeed, NewsStatus, NewsImage

class NewsFeedSerializer(serializers.ModelSerializer):
</code></pre>

<p>Up to this, it's working fine I'm able to use models in serializer. Now I have to use serializer in my NewsfeedModel file, so I import serializer in NewsfeedModel file, but it's not allowing me to use.</p>

<p>getting following error in the log:</p>

<pre><code>ImportError: cannot import name NewsFeed
</code></pre>

<p>Is there any way to use serializer in model class?</p>

<p>The way to work around a circular import is to remove one of the imports from the module level and do it inside the method where it is used. </p>

<p>You haven't shown all the model code so I don't know where you use it, but if it's in <code>save</code> it would look like this:</p>

<pre><code>def save(self, **kwargs):
    import serializers
    # rest of method
</code></pre>

python django serialization django-models django-rest-framework