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>It seems like you are trying to Import NewsFeed model to itself.</p>

<p>The flow of a django-rest-framework looks like this:</p>

<p>Model > Serializer > View</p>

<p>After getting your serializers done all you need to do Is to import the models and serializers into the views.py file, in which you will create classes/functions to handle the calls to the API. an example for such use will be:</p>

<p>views.py</p>

<pre><code>from newsfeedmodel.py import *
from serializers.pi import *

class NewsFeedViewSet(viewsets.ModelViewSet):
    queryset = NewsFeed.objects.all()
    serializer_class = NewsFeedSerializer
</code></pre>

<p>I would reccomend that you read the DRF documentation for better understanding:<br>
<a href="http://django-rest-framework.org" rel="nofollow">http://django-rest-framework.org</a></p>

python django serialization django-models django-rest-framework