我想用django表來創建一個可排序的表格出一些模型。django_tables和通過相關經理向後一對一的關係
http://elsdoerfer.name/docs/django-tables/types/models.html#spanning-relationships說,我可以跨越關係,但我很難做到「倒退」。
這裏是我的模型和表:
class Book(Model):
isbn = ISBNField("ISBN", primary_key=True, editable=True, blank=True, null=False)
class SomeInfo(Model):
book = OneToOneField(Book, primary_key=True, editable=False, blank=False, null=False, verbose_name = _('ISBN'), related_name='someinfo')
title = CharField(_("Title"), editable=False, blank=False, null=True, max_length=250)
class MyInfo(Model):
book = OneToOneField(Book, primary_key=True, editable=False, blank=False, null=False, verbose_name = _('ISBN'), related_name='myinfo')
average_price = DecimalField(_("Average price"), editable=False, null=True, max_digits=6, decimal_places=2)
class Query(Model):
book = ForeignKey(Book, editable=True, blank=True, null=True, verbose_name = _('ISBN'))
max_price = DecimalField(_('Maximum price'), null=True, max_digits=6, decimal_places=2)
class MyTable(ModelTable):
my_average_price = tables.Column(data="book__myinfo__average_price")
my_title = tables.Column(data="book__someinfo__title")
class Meta:
model = Query
當試圖在模板中使用例如my_average_price,我得到這個錯誤:
TemplateSyntaxError at /list/
Caught ValueError while rendering: Could not resolve myinfo from book__myinfo__average_price
任何想法,使如何工作的呢?
所有列,也是相關關係中的列,都需要進行排序。
是的,事實確實如此。另一種顯示此錯誤的情況是MyInfo上的OneToOneField不是primary_key,並且沒有唯一約束,並且碰巧存在多於一個MyInfo對象的給定書籍。 – miernik 2011-02-03 15:00:41