How to use TabularInline with editable fields in ManyToMany relationships?

My models contain many-to-many relationships. 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 embed Measurement fields in the DataSet admin, for example, how TabularInline works with the ForeignKey field. This is what I still have:

 # 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 is pop-ups with the β€œ+” buttons next to them that open the Measurement admin. I want the actual field of the value dimension to be displayed in a row. I tried adding value to the list of fields in MeasurementInline:

 # admin.py class MeasurementInline(admin.TabularInline): model = Measurement.sets.through fields = ['value'] 

But this gives me an error: 'MeasurementInline.fields' refers to field 'value' that is missing from the form. .

How can I open editable fields for Measurement in the admin DataSet ?

Notes: This is a simplified case; my real case has many fields in my Measurement model. It would be terribly tiring if people using the admin interface had to open a new window for entering data, especially since they would need to do some copying and pasting between the fields.

Even in my actual models, the data that I want users to edit inline does NOT describe the relationship between the DataSet and Measurement β€” only Measurement itself. I believe that this makes the intermediate model unsuitable for my purposes.

+7
source share
2 answers

Short answer: you cannot.

Long answer: you cannot without significant editing django ModelAdmin. The InlineFormset plants he uses are extremely limited and cannot handle ManyToManyInlines. InlineModelAdmin objects are only supported using ForeignKeys.

Unfortunately.

+5
source

Well, not sure to really understand the project you're working on, but if you want to measure inlines in a dataset, you might want to establish a relationship 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 it’s simple:

 class MeasurementInline(admin.TabularInline): model = Measurement class DataSetAdmin(admin.ModelAdmin): inlines = [MeasurementInline] admin.site.register(DataSet, DataSetAdmin) 

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

-one
source

All Articles