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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With