在 django 中跨通用关系排序
举个例子:
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Container(models.Model):
pass
class StoredUnit(models.Model):
container = models.ForeignKey( Container )
content_type = models.ForeignKey( ContentType )
object_id = models.IntegerField()
content_object = generic.GenericForeignKey()
class UnitA(models.Model):
name = models.CharField()
stored_unit = generic.GenericRelation( StoredUnit )
class UnitB(models.Model):
name = models.CharField()
stored_unit = generic.GenericRelation( StoredUnit )
给定一个容器,是否可以通过通用关系中的字段对“Unit”进行排序?
e.g.
container.storedunit_set.order_by('<genericly_related_unit>__name')
For the example:
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Container(models.Model):
pass
class StoredUnit(models.Model):
container = models.ForeignKey( Container )
content_type = models.ForeignKey( ContentType )
object_id = models.IntegerField()
content_object = generic.GenericForeignKey()
class UnitA(models.Model):
name = models.CharField()
stored_unit = generic.GenericRelation( StoredUnit )
class UnitB(models.Model):
name = models.CharField()
stored_unit = generic.GenericRelation( StoredUnit )
Given a container, is it possible to sort the 'Unit's by a field across a generic relationship?
e.g.
container.storedunit_set.order_by('<genericly_related_unit>__name')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论