2016-08-03 24 views
0

我通過DRF製作了django項目。在DRF(django-rest-framework)中,如果模型有兩個foreignkeyfield,是否可以序列化?

在我與DRF的鬥爭中,我越來越想知道可以序列化這些模型。

models.py

class Post(models.Model): 
    author = models.ForeignKey(User, related_name='related_postwriter') 
    title = models.CharField(max_length=200, blank = False) 
    text = models.TextField(blank = True) 
    created_date = models.DateTimeField(
     default=timezone.now 
     ) 
    point = models.PointField(null=False, blank=True) 

    def __str__(self):    # __unicode__ on Python 2 
     return self.title 

class Comment(models.Model): 
    post = models.ForeignKey('blog.Post', related_name='related_comments') 
    author = models.ForeignKey(User, related_name='related_commentwriter') 
    text = models.TextField(max_length=500) 
    created_date = models.DateTimeField(default=timezone.now) 

    def __str__(self): 
     return self.text 

有在評論模式二ForeignKey的領域,我認爲這是可能serializer.py的原因是很難對付。

serializers.py

class UserSerializer(serializers.ModelSerializer): 
    posts = serializers.PrimaryKeyRelatedField(many=True, read_only=True) 
    comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True) 
    class Meta: 
     model = User 
     fields = ('id', 'username', 'email', 'posts') 

class PostSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Post 
     fields = ('id', 'author', 'title', 'text', 'point') 
    def create(self, validated_data): 
     validated_data['author'] = self.context['request'].user 
     return super(PostSerializer, self).create(validated_data) 


class CommentSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Comment 
     fields = ('id', 'post', 'author', 'text') 
    def create(self, validated_data): 
     validated_data['author'] = self.context['request'].user 
     return super(CommentSerializer, self).create(validated_data) 

我想comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True) 已被放置在class PostSerializer(serializers.ModelSerializer):也,但是當我把我想,總會有錯誤AssertionError at /posts/ The field 'comments' was declared on serializer PostSerializer, but has not been included in the 'fields' option.所以,當我把「意見」的療法領域像

class PostSerializer(serializers.ModelSerializer): 
    comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True) 
    class Meta: 
     model = Post 
     fields = ('id', 'author', 'title', 'text', 'point', 'comments') 

然後, - >AttributeError at /posts/ 'Post' object has no attribute 'comments'發生。

我的錯是什麼?

因爲我想製作Android應用程序,用戶可以在同一時間閱讀帖子和評論「與帖子相關」(如幾乎社區網站,reddit,facebook ...),serializers.py我發佈的是不夠。 serializers.py我發佈似乎不足以反映發表和評論之間的關係。

評論模型有問題嗎?還是我錯了?

我是新手,編程新手,請教我。

感謝您的閱讀。

回答

2

沒有什麼錯,你只是訪問錯誤的屬性。如果你看一下你的模型,你定義一個相關名稱的帖子:

post = models.ForeignKey('blog.Post', related_name='related_comments')

所以,當你寫你的序列化,「田」你要訪問應該是相關的名稱,而不是評論。串行器更改爲:

class PostSerializer(serializers.ModelSerializer): 
    related_comments = serializers.PrimaryKeyRelatedField(many=True, read_only=True) 
    class Meta: 
     model = Post 
     fields = ('id', 'author', 'title', 'text', 'point', 'related_comments') 
+0

謝謝!有用! – touchingtwist

相關問題