基于不同模型的模型顺序

2024-04-26 17:45:04 发布

您现在位置:Python中文网/ 问答频道 /正文

在Django,我有两个模型;模型B与模型a的主键具有主键OneToOneField关系

class A(models.Model):
    id = models.AutoField(primary_key=True)
    id2 = models.ForeignKey(also a autofield to the field it’s referencing)
    class Meta:
        ordering = [“-id2” , “id”]

class B(models.Model): 
    id = models.OneToOneField(A, primary_key=True)
    class Meta:
        ordering = [???]

有没有一种方法可以让我在不复制数据的情况下根据a型订购B型


Tags: djangokey模型idtruemodel关系models
1条回答
网友
1楼 · 发布于 2024-04-26 17:45:04

试试这个

class A(models.Model):
    id = models.AutoField(primary_key=True)
    id2 = models.ForeignKey(also a autofield to the field it’s referencing)
    class Meta:
        ordering = [“-id2” , “id”]

class B(models.Model): 
    id = models.OneToOneField(A, primary_key=True,related_name="A")
    class Meta:
        ordering = ["A__id"]

相关问题 更多 >