I have 3 models: Document, MetaData, MetaDataValue, and a "connector table" DocumentMetaDataValue. I want to add a document and all the associated metadata values on one admin page.
models.py:
# MetaData
class MetaData(models.Model):
metadata_id = models.AutoField(primary_key = True)
name = models.CharField('metadata name', max_length=200, unique=True)
description = models.TextField('description')
def __str__(self):
return self.name
# MetaData Value
class MetaDataValue(models.Model):
metadata_id = models.ForeignKey(MetaData, on_delete=models.CASCADE,)
value = models.CharField('value', max_length=200, unique=True)
def __str__(self):
return self.value
# Document
class Document(models.Model):
document_id = models.AutoField(primary_key=True)
metadata = models.ManyToManyField('MetaData', through='DocumentMetaDataValue', through_fields=('document', 'metadata'))
metadatavalue = models.ManyToManyField('MetaDataValue', through='DocumentMetaDataValue', through_fields=('document', 'metadataValue'))
class DocumentMetaDataValue(models.Model):
document = models.ForeignKey(Document, on_delete=models.CASCADE)
metadata = models.ForeignKey(MetaData, on_delete=models.CASCADE)
metadataValue = models.ForeignKey(MetaDataValue, on_delete=models.CASCADE)
admin.py:
class Fred(forms.ModelForm):
""" something newer
"""
class Meta:
model = DocumentMetaDataValue
fields = '__all__'
class DocumentMetaDataValueInline(admin.TabularInline):
model = DocumentMetaDataValue
form = Fred
class DocumentAdmin(admin.ModelAdmin):
form = DocumentForm
list_display = ('get_document_type', 'document_state', 'title', 'description', 'original_file_name', 'created', 'storage_file_name', 'get_thumb', )
ordering = ('created',)
readonly_field = ('document_state',)
filter_horizontal = ('metadata', 'metadatavalue', )
inlines = (DocumentMetaDataValueInline, )
# other code not related to this problem
admin.site.register(Document, DocumentAdmin)
There are 16+ metadata names, and each one has one or more metadata values. For example, the metadata name Person has 23 values ('Bob', 'Sam', 'Sally', etc), and the metadata name Decade has 12+ values (1890, 1900, 1910, 1920, etc). A Document can have one or more metadata names associated with it, and one or more metadata values for that metadata name can be associated with that Document. For example, a Document can be a photograph with two people in it. The metadata value for Decade could be 1910, and the metadata values for Person could be Sam and Sally.
On each line of the inline, I want to show the metadata name opposite a drop down with the associated metadata values. One should be able to pick more than one metadata value for a particular metadata name.
What happens in the code above is each line of the inline has two drop down lists - the first is all the metadata names and the second is all the metadata values.
I want to change the first drop down to be a single string for a metadata name, and the second drop down to still be a drop down, but only the values for that metadata name. I don't see how to make these changes.
Thanks!
Mark