django_tables 并通过相关管理器向后遵循一对一关系
我正在尝试使用 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
任何想法如何使其工作?
所有列,包括相关关系中的列,都需要可排序。
I am trying to use django-tables to create a sortable table out of some Models.
http://elsdoerfer.name/docs/django-tables/types/models.html#spanning-relationships says that I can span through relationships, but I have difficulty doing it "backwards".
Here are my models and the table:
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
When trying to use for example my_average_price in a template, I get this error:
TemplateSyntaxError at /list/
Caught ValueError while rendering: Could not resolve myinfo from book__myinfo__average_price
Any ideas how to make it work?
All columns, also the ones from the related relationships, need to be sortable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果存在相关的 SomeInfo 或 MyInfo 实例,这对我有用。仅当情况并非如此时才应引发错误。即,表中的每本书都需要附加 SomeInfo 和 MyInfo。
这是因为如果链中的某个属性不存在,
django_tables/models.py:BoundModelRow._default_render
中的代码将引发此错误,如果缺少相关实例,就是这种情况:如果 Book 实例不存在 MyInfo,则 Book.myinfo 不会作为属性存在。这可能需要被视为 django-tables 中的错误。它不应该仅仅反转对象属性,而应该查看实际定义的数据库字段的instance._meta。如果它确定存在反向 OneToOne 访问器,但没有相应的属性,则它只会假定 null 值而不是引发异常。
我已将其报告为错误: https://github.com/miracle2k /django-tables/issues/issue/11 但是,我不确定何时开始处理它。补丁当然是受欢迎的。
This works for me, provided that a related SomeInfo or MyInfo instance exists. The error should only be raised if that is not the case. I.e., each book in the table would need to have SomeInfo and MyInfo attached.
This is because the code in
django_tables/models.py:BoundModelRow._default_render
will raise this error if one of the attributes in the chain does not exist, which is the case here if a related instance is missing: Book.myinfo does not exist as an attribute if no MyInfo exists for the Book instance.This would probably need to be considered a bug in django-tables. Rather than just reversing through the object attributes, it should look at instance._meta at which database fields have actually been defined. If it determines that a reverse OneToOne accessor exists, but no corresponding attribute does, it would then just assume a null value rather than raising an exception.
I've reported this as a bug here: https://github.com/miracle2k/django-tables/issues/issue/11 However, I'm not sure when I'll get to work on it. Patches are certainly welcome.