基于不同模型的模型顺序

时间:2019-05-12 18:27:27

标签: python django postgresql django-models

在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,而无需复制数据?

1 个答案:

答案 0 :(得分:0)

尝试这个

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"]