django models.py使用外地字段排序

时间:2015-09-22 09:56:00

标签: python django django-models django-1.7

我使用的是django1.7,我有两个型号。

我的一个模型我想通过外国模型上的字段指定ordering。这两个模型由外键链接。

以下是我的两个示例模型:

class ObjectiveDetails(models.Model, FillableModelWithLanguageVersion):
    user = models.ForeignKey(User)
    language_version = models.ForeignKey('LanguageVersion')
    field = models.TextField(null=False, blank=False, max_length=5000)
    objective_timestamp_added = models.DateTimeField(auto_now_add=True, auto_now=False)
    objective_timestamp_updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    class Meta:
        ordering = ['language_version.language_code', 'id']

class LanguageVersion(models.Model):
    """Language version selection for a user"""
    user = models.ForeignKey(User)
    language_code = models.CharField(max_length=32)
    language_code_disabled = models.BooleanField(default=False)
    language_version_timestamp_added = models.DateTimeField(auto_now_add=True, auto_now=False)
    language_version_timestamp_updated = models.DateTimeField(auto_now=True, auto_now_add=False)  # the date the language version is updated when the user changes their subscription type.

我试图通过LanguageVersion模型上的language_code来排序ObjectiveDetails。

我尝试了几次,但我无法做到。我已阅读文档并搜索了SO&谷歌,但找不到正确的答案,

1 个答案:

答案 0 :(得分:2)

将language_code的排序添加到 LanguageVersion ,并从 ObjectiveDetails

中删除.language_code
  

您添加的每个外键都将   隐含地包括其所有默认排序。

https://docs.djangoproject.com/en/1.7/ref/models/options/#ordering

你的模型将是这样的

Unexpected failure in getValuePostProcess

Exception Id: qccore.general-error