How do I use a TabularInline with editable fields on a ManyToMany relationship?

别说谁变了你拦得住时间么 提交于 2019-12-01 04:21:21

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.

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 ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!