我正在使用 Django REST API 和generic_relations模块来创建 Django Comments 的一种变体,它不是由模板驱动,而是通过 REST API 函数。
名为 Annotation 的 Model 类如下所示:
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='annotation: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
atget_raw_data_form(self, view, method, request)
调用中找不到对象obj = getattr(view, 'object', None)
。
这反过来,在评估field_to_native
函数 as serializer = self.determine_deserializer_for_data(value)
in 时generic_relations
,会导致 as 不传递任何内容,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