如何在 ManyToMany 关系上使用具有可编辑字段的 TabularInline?

发布于 2024-11-03 06:12:19 字数 1596 浏览 4 评论 0原文

我的模型包含多对多关系。 测量可以是任意数量的数据集的一部分。

# 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 字段,就像 TabularInlineForeignKey 字段一起使用一样。这就是我到目前为止所拥有的:

# 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 模型中有很多字段。如果使用管理界面的人必须打开一个新窗口来输入数据,那将是非常乏味的,特别是因为他们还需要在字段之间进行一些复制和粘贴。

即使在我的现实模型中,我希望用户内联编辑的数据也不会描述 DataSetMeasurement 之间的关系 - 仅描述 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若相惜即相离 2024-11-10 06:12:19

简短的回答:你不能。

长答案:如果不大量编辑 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.

り繁华旳梦境 2024-11-10 06:12:19

好吧,不确定是否真正理解您正在从事的项目,但如果您希望在数据集中测量inlines,您可能需要将关系放入数据集模型中:

class DataSet(models.Model):
    purpose = models.TextField()
    measurements = models.ManyToManyField(DataSet, null=True, blank=True)

class Measurement(models.Model):
    value = models.IntegerField()

在您的 admin.py 中,只需:

class MeasurementInline(admin.TabularInline):
    model = Measurement

class DataSetAdmin(admin.ModelAdmin):
    inlines = [MeasurementInline]

admin.site.register(DataSet, DataSetAdmin)

使用 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:

class DataSet(models.Model):
    purpose = models.TextField()
    measurements = models.ManyToManyField(DataSet, null=True, blank=True)

class Measurement(models.Model):
    value = models.IntegerField()

And in your admin.py, simply :

class MeasurementInline(admin.TabularInline):
    model = Measurement

class DataSetAdmin(admin.ModelAdmin):
    inlines = [MeasurementInline]

admin.site.register(DataSet, DataSetAdmin)

And using model = Measurement.sets.through looks odd to me.But maybe I totally missed the point ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文