如何在 ManyToMany 关系上使用具有可编辑字段的 TabularInline?
我的模型包含多对多关系。 测量
可以是任意数量的数据集
的一部分。
# models.py
from django.db import models
class DataSet(models.Model):
purpose = models.TextField()
class Measurement(models.Model):
value = models.IntegerField()
sets = models.ManyToManyField(DataSet, null=True, blank=True,
verbose_name="datasets this measurement appears in")
我希望我的管理界面能够内联 DataSet
管理中的 Measurement
字段,就像 TabularInline
与 ForeignKey
字段一起使用一样。这就是我到目前为止所拥有的:
# admin.py
from django.contrib import admin
from myapp.models import Measurement, DataSet
class MeasurementInline(admin.TabularInline):
model = Measurement.sets.through
class DataSetAdmin(admin.ModelAdmin):
inlines = [MeasurementInline]
admin.site.register(DataSet, DataSetAdmin)
不幸的是,我得到的只是下拉框,旁边有“+”按钮,可以打开测量管理。我希望在内联中公开实际的测量字段值
。我尝试将value
添加到MeasurementInline上的字段列表中:
# admin.py
class MeasurementInline(admin.TabularInline):
model = Measurement.sets.through
fields = ['value']
但这给了我一个错误:'MeasurementInline.fields'指的是表单中缺少的字段'value' .
。
如何在 DataSet
管理中公开 Measurement
的可编辑字段?
笔记: 这是一个简化的案例;我的真实案例的 Measurement
模型中有很多字段。如果使用管理界面的人必须打开一个新窗口来输入数据,那将是非常乏味的,特别是因为他们还需要在字段之间进行一些复制和粘贴。
即使在我的现实模型中,我希望用户内联编辑的数据也不会描述 DataSet
和 Measurement
之间的关系 - 仅描述 Measurement< /代码>本身。我相信这使得中介模型不适合我的目的。
My models contain a many-to-many relationship. Measurements
can be part of any number of DataSets
.
# models.py
from django.db import models
class DataSet(models.Model):
purpose = models.TextField()
class Measurement(models.Model):
value = models.IntegerField()
sets = models.ManyToManyField(DataSet, null=True, blank=True,
verbose_name="datasets this measurement appears in")
I want my admin interface to inlines Measurement
fields in the DataSet
admin like how a TabularInline
works with a ForeignKey
field. This is what I have so far:
# admin.py
from django.contrib import admin
from myapp.models import Measurement, DataSet
class MeasurementInline(admin.TabularInline):
model = Measurement.sets.through
class DataSetAdmin(admin.ModelAdmin):
inlines = [MeasurementInline]
admin.site.register(DataSet, DataSetAdmin)
Unfortunately, all I get are dropdown boxes with "+" buttons next to them that open up the Measurement admin. I want the actual Measurement field value
to be exposed in the inline. I tried adding value
to the list of fields on the MeasurementInline:
# admin.py
class MeasurementInline(admin.TabularInline):
model = Measurement.sets.through
fields = ['value']
But that gives me an error: 'MeasurementInline.fields' refers to field 'value' that is missing from the form.
.
How do I expose editable fields for Measurement
within the DataSet
admin?
Notes:
This is a simplified case; my real case has many fields in its Measurement
model. It would be terribly tedious if the people using the admin interface had to open up a new window to enter data, especially since they will need to do some copying and pasting between fields as well.
Even in my real-world models, the data I want users to edit inline does NOT describe the relationship between the DataSet
and the Measurement
-- only the Measurement
itself. I believe this makes an intermediary model unsuitable for my purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:你不能。
长答案:如果不大量编辑 django 的 ModelAdmin,你就无法做到这一点。它使用的 InlineFormset 工厂极其有限,目前无法处理 ManyToManyInlines。 InlineModelAdmin 对象仅受ForeignKeys 支持。
对不起。
The short answer: you can't.
The long answer: you can't without significantly editing django's ModelAdmin. The InlineFormset factories that it uses are extremely limited, and cannot currently handle ManyToManyInlines. InlineModelAdmin objects are only supported with ForeignKeys.
Sorry.
好吧,不确定是否真正理解您正在从事的项目,但如果您希望在数据集中测量
inlines
,您可能需要将关系放入数据集模型中:在您的 admin.py 中,只需:
使用
model =Measurement.sets.through
对我来说看起来很奇怪。但也许我完全没有抓住重点?Well, not sure to really understand the project you are working on but if you want Measurement
inlines
in Dataset, you may want to put the relation in the Dataset model:And in your admin.py, simply :
And using
model = Measurement.sets.through
looks odd to me.But maybe I totally missed the point ?