Search code examples
pythondjangodjango-rest-frameworkgeneric-foreign-keygeneric-relations

ValidationError with Django REST API and generic_relations for generic foreign key writable access


I'm using Django REST API and generic_relations module with it to create a kind of variant to Django Comments which is not driven by templates but via REST API functions.

The Model class in named Annotation and is like:

class Annotation(BaseCommentAbstractModel):

    paragraph_id = models.PositiveIntegerField(null=False)

    body = models.TextField()
    '''
    Annotations can be written only by logged in users. It is to prevent hit and run comments by people under anonymity.
    '''
    user = models.ForeignKey(DjangoUser, null=False, verbose_name=_('user'), related_name = "annotations")

Now, there is another model, for content, which can be pretty much anything, so lets say, it is a simple Posting Application:

class BlogContent(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    body = models.TextField()
    ...
    annotation = GenericRelation(Annotation, content_type_field='content_type', object_id_field='object_pk')

The serializers for the same look like:

from generic_relations.relations import GenericRelatedField 

class BlogContentSerializer(serializers.ModelSerializer):
    author_id = serializers.Field(source='author_id.username')
    annotation = serializers.RelatedField(many=True)

    class Meta:
        model = BlogContent
        fields = ('title','created','author_id',
              'data','published_flag', 'url_path','slug','annotation')

class AnnotationSerializer(serializers.ModelSerializer):
    user = serializers.Field(source='user.username')
    content_object = GenericRelatedField({
        BlogContent: serializers.HyperlinkedRelatedField(view_name='annotation:blogcontent-detail'),
        }, read_only=False)

    class Meta:
        model = Annotation
        fields = ('paragraph_id', 'body', 'user', 'content_object')


class UserSerializer(serializers.ModelSerializer):
    blogcontent= serializers.PrimaryKeyRelatedField(many=True) 
    annotations = serializers.PrimaryKeyRelatedField(many=True)

    class Meta:
        model = User
        fields = ('username', 'id', 'blogcontent', 'annotations')

Now, the problem is in the line: content_object = GenericRelatedField({ BlogContent: serializers.HyperlinkedRelatedField(view_name='annotation:blogcontent-detail'), }, read_only=False)

If the generic relations are to be used as read only, by setting the read_only=True, then the REST API browsability is fine, but the moment I switch it to False, the server throws back a ValidationError

ValidationError at /en/annotation/
[u'Invalid model - model not available.']

Perusing the logs and the code, it seems that in rest_framework.renderers.py at get_raw_data_form(self, view, method, request) Does not find an object in the call obj = getattr(view, 'object', None).

This in turn, while evaluating field_to_native function as serializer = self.determine_deserializer_for_data(value) in generic_relations, causes to pass nothing in as value and hence, the code breaks.

Has anybody else too encountered such error and/or could help me fix it? Please help!

The traceback is as follows:

/home/craft/pirateenv/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
            response = response.render() ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/django/template/response.py in render
        self.content = self.rendered_content ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/response.py in rendered_content
    ret = renderer.render(self.data, media_type, context) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/renderers.py in render
    context = self.get_context(data, accepted_media_type, renderer_context) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/renderers.py in get_context
    raw_data_post_form = self.get_raw_data_form(view, 'POST', request) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/renderers.py in get_raw_data_form
            content = renderer.render(serializer.data, accepted, context) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/serializers.py in data
            self._data = self.to_native(obj) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/rest_framework/serializers.py in to_native
        value = field.field_to_native(obj, field_name) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/generic_relations/relations.py in field_to_native
    serializer = self.determine_deserializer_for_data(value) ...
▶ Local vars
/home/craft/pirateenv/lib/python2.7/site-packages/generic_relations/relations.py in determine_deserializer_for_data
        raise ValidationError(self.error_messages['no_model_match']) ...
▶ Local vars

Solution

  • This issue on the repository also mentions the same error but in a different context, the solution offered, nevertheless works for me too, until the time the author releases a fix.

    The solution is to add an if value: clause after calling the super method.