在多对多字段上启用 Django 管理过滤器
我有一个简单的 Django 模型,类似于:
class Address(models.Model):
blah
class MemberData(models.Model):
user = models.ForeignKey(User)
addresses = models.ManyToManyField(Address)
我想在管理中公开地址模型,以允许用户按关联用户过滤地址记录。例如,
class AddressAdmin(admin.ModelAdmin):
model = Address
list_filter = [???]
ModelAdmin.list_filter 属性允许这样做,但我不确定使用什么字段名称来支持我的多对多关系。如果 Address 模型直接引用 MemberData 模型,我可以执行以下操作:
class AddressAdmin(admin.ModelAdmin):
model = Address
list_filter = ['memberdata__user']
间接多对多关系是否有等效语法?如果没有,是否有任何解决方法可以达到相同的目的?
I have a simple Django model resembling:
class Address(models.Model):
blah
class MemberData(models.Model):
user = models.ForeignKey(User)
addresses = models.ManyToManyField(Address)
I want to expose the Address model in admin to allow a user to filter Address records by their associated user. e.g.
class AddressAdmin(admin.ModelAdmin):
model = Address
list_filter = [???]
The ModelAdmin.list_filter property allows this, but I'm not sure what field name to use to support my many-to-many relationship. If the Address model has a direct reference to the MemberData model, I could do something like:
class AddressAdmin(admin.ModelAdmin):
model = Address
list_filter = ['memberdata__user']
Is there any equivalent syntax for an indirect many-to-many relationship? If not, is there any workaround to accomplish the same end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信在这种情况下,您可以为 M2M 关系创建一个简单的
through
模型,然后使用正常语法来遵循(现在是明确的)ForeignKey
。类似于:在管理中:
I believe in a case like this, you could make a trivial
through
model for your M2M relation, and then use the normal syntax to follow the (now explicit)ForeignKey
s. Something like:and in the admin:
我正在使用 1.5,
list_filter = ['memberdata__user']
似乎可以工作。I'm using 1.5 and
list_filter = ['memberdata__user']
seems like it would work.