django models.py 使用外部字段排序
django models.py ordering using foreign field
我使用的是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 & Google,但找不到正确的答案,
将按 language_code 的排序添加到 LanguageVersion 并从 ObjectiveDetails
中删除 .language_code
Each foreign key you add will
implicitly include all of its default orderings as well.
https://docs.djangoproject.com/en/1.7/ref/models/options/#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', '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.
class Meta:
ordering = ['language_code']
我使用的是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 & Google,但找不到正确的答案,
将按 language_code 的排序添加到 LanguageVersion 并从 ObjectiveDetails
中删除 .language_codeEach foreign key you add will implicitly include all of its default orderings as well.
https://docs.djangoproject.com/en/1.7/ref/models/options/#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', '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.
class Meta:
ordering = ['language_code']