如何与 Django REST 模型建立 Parent/Child 关系?

How to create a Parent/Child relationship with Django REST models?

我是 Django REST 的新手,想基本上创建一个 join table like this.

一个页面(txt 文档)可以有父页面和子页面。每个页面都可以是多个其他页面的父页面和子页面。

Page Entity 和 PageToPage Entity 在后端都有一个已实现的模型和序列化程序,其中 serializers.py 如下所示:

class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = ['pkid', 'title', 'text', 'creation_date', 'page_of_user']

class PageToPageSerializer(serializers.ModelSerializer): 
      parent_page = PageSerializer(many=True, read_only=True)
      child_page = PageSerializer(many=True, read_only=True)
      class Meta:
          model = PageToPage
          fields = ['pkid', 'parent_page', 'child_page']

models.py

class Page(models.Model):
    pkid = models.BigAutoField(primary_key=True, editable=False)
    page_of_user = models.ForeignKey(User, related_name='pages', on_delete=models.CASCADE)
    title = models.TextField(blank=True, default='')
    text = models.TextField(blank=True, default='')
    creation_date = models.DateField(auto_now_add=True)

class PageToPage(models.Model):
    pkid = models.BigAutoField(primary_key=True, editable=False)
    parent_page = models.ForeignKey(Page, related_name='parent_page', on_delete=models.CASCADE, blank=True, null=True)
    child_page = models.ForeignKey(Page, related_name='child_page', on_delete=models.CASCADE, blank=True, null=True)

当输入 manage.py shell 并创建 PageToPage 对象时,它只会产生空结果:

# Create Pages
p1 = Page(title="Parent Page 1", text="Once upon a time, there was a young man...", page_of_user=u2)
p1.save()
p3 = Page(title="Child Page 1", text="Once upon a time, there was a young man...", page_of_user=u2)
p3.save()

# Create Page Relationships
ptp = PageToPage(parent_page=p1, child_page=p3)
ptp.save()

# Select all Pages
print("Created Pages:")
ser2 = PageSerializer(Page.objects.all(), many=True)
print(ser2.data)

# Select all Page to Page relationships
ser_ptp = PageToPageSerializer(PageToPage.objects.all())
print("Created Page relationships:")
print(ser_ptp.data)

输出:

>>> Created Pages:
>>> [OrderedDict([('pkid', 1), ('title', 'Test Page number 1'), ('text', 'Just some sample text'), ('creation_date', '17.06.2021'), ('page_of_user', UUID('eeec0437-75f2-44ba-935d-96fcb78e38c6'))]), ...]
>>> Created Page relationships:
>>> {}

我做错了什么?非常感谢任何帮助!

解决方法: 我删除了我的 PageToPage 内容并按照@Jimmar 的建议更改了我的 PageModel 和 Serializer:

class Page(models.Model):
    pkid = models.BigAutoField(primary_key=True, editable=False)
    page_of_user = models.ForeignKey(User, related_name='pages', on_delete=models.CASCADE)
    title = models.TextField(blank=True, default='')
    text = models.TextField(blank=True, default='')
    creation_date = models.DateField(auto_now_add=True)
    parent_p = models.ManyToManyField('self', related_name='parent_page', blank=True, symmetrical=False)
    child_p = models.ManyToManyField('self', related_name='child_page', blank=True, symmetrical=False)

请注意,我在 ManyToManyField 中添加了 symmetrical=False 以维持生计。