我正在使用Django REST API和generic_relations模块来创建一种Django Comments的变体,它不是由模板驱动的,而是通过REST API函数。
Model类命名为Annotation,类似于:
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")
现在,有另一个模型,内容,它可以是几乎任何东西,所以我们说,它是一个简单的张贴应用程序:
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')
相同的序列化器如下:
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')
现在,问题在这一行:content_object = GenericRelatedField({BlogContent: serializers.HyperlinkedRelatedField (view_name = '注释:blogcontent-detail '),}, read_only = False)
如果通用关系被用作只读,通过设置read_only=True
,那么REST API的可浏览性是好的,但是当我切换到False
时,服务器抛出一个ValidationError
ValidationError at /en/annotation/
[u'Invalid model - model not available.']
仔细阅读日志和代码,似乎在rest_framework.renderers.py
中get_raw_data_form(self, view, method, request)
没有在调用obj = getattr(view, 'object', None)
中找到对象。
这反过来,当field_to_native
函数在generic_relations
中作为serializer = self.determine_deserializer_for_data(value)
求值时,导致没有作为value
传入,因此,代码中断。
有其他人也遇到这样的错误和/或可以帮助我修复它吗?请帮助!
回溯如下:
/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
存储库上的这个问题也提到了相同的错误,但在不同的上下文中,提供的解决方案仍然适用于我,直到作者发布修复。
解决方法是在调用super
方法后添加if value:
子句。