我基本上试图做与这个问题中描述的相反的事情:Django: filtering order_by a generic relationship
class Translation(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey('content_type', 'object_id')
field = models.CharField(max_length=64) #field from the translated model
language = models.CharField(max_length=8, choices=settings.LANGUAGES, verbose_name=_("language"))
text = models.TextField() #translation
class Category(models.Model):
translations = generic.GenericRelation(Translation)
name = models.CharField(max_length=128, verbose_name=_("Name"))
slug = models.SlugField(blank=True, default="", verbose_name=_("Slug"))
我想检索所有翻译并按类别名称排序。像这样的东西:
Translation.objects.all().order_by('object__name')
但这不起作用,因为object
它不是数据库中的列。尝试订购content_type__name
也不起作用。