Tuesday, June 21, 2016

django rest framework - request context key error

I have two models (Like and News). I am using django-rest-framework to make a web api out of it.

class Like(models.Model):
    user 
= models.ForeignKey(User)
    content_type 
= models.ForeignKey(ContentType)
    object_id 
= models.PositiveIntegerField()
    content_object 
= GenericForeignKey('content_type', 'object_id')

class News(models.Model):
    user 
= models.ForeignKey(User)
    title 
= models.CharField(max_length=150)
    
...
    likes 
= GenericRelation(Like)


Like object has its own user field to store who liked the News object. Now to check if a specific user exists in any of the likes of a News object, I am getting request.user from a SerializerMethodField.

class NewsSerializer(serializers.HyperlinkedModelSerializer):
    user 
= UserSerializer()
    likes_count 
= serializers.IntegerField(source='likes.count', read_only=True)
    user_in_likes 
= serializers.SerializerMethodField()

    
class Meta:
        model 
= News
        fields 
= ('user', 'title', 'body', 'article_image', 'pub_date', 'likes_count', 'user_in_likes')

    
def get_user_in_likes(self, obj):
        user 
= self.context['request'].user
        what 
= obj.likes.filter(user=user).exists()
        
return what


When I go the /news/ url, I get the json objects including the user_in_likes to true/false for each news object.

However, I have another serialzer for different model which imports NewsSerializer class and other similar serializers:

class ActivityObjectRelatedField(serializers.RelatedField):
    
def to_representation(self, value):
        
if isinstance(value, User):
            serializer 
= UserSerializer(value)
        
elif isinstance(value, Job):
            serializer 
= JobSerializer(value)
        
elif isinstance(value, News):
            serializer 
= NewsSerializer(value)
        
elif isinstance(value, Tender):
            serializer 
= TenderSerializer(value)
        
else:
            
raise Exception('Unexpected type of tagged object')
        
return serializer.data    

class ActivitySerializer(serializers.HyperlinkedModelSerializer):
    actor 
= ActivityObjectRelatedField(read_only=True)
    target 
= ActivityObjectRelatedField(read_only=True)

    
class Meta:
        model 
= Activity
        fields 
= ('url', 'actor', 'verb', 'target', 'pub_date')


Now if I visit /activities/, to get the activities objects I am getting an error:

KeyError at /activities/

'request'


And it points to the line of SerializerMethod of NewsSerialize class where self.context['request'].user is used.

Exception Location: /vagrant/myproject/news/serializers.py in get_user_in_likes, line 25


Again if I visit /news/ url, everything is fine and I get news objects. What am I missing here? Why is request not being recognized in the ActivitiesSerializer class? Please help me solve this problem. Thank you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWsBp3uXaMVzqLmHmyQGP3MtwO06nZ-GQE98523DAVD-8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment