I have a class Document that uploads a document. I have a class MetaData that is the name for some metadata for that document. Since a MetaData can have one or more values, there is another class called MetaDataValue that has a ForeignKey to MetaData. Finally, there is a class DocumentMetaDataValue that links these three tables. It almost works...but in the Admin screen when I want to add something to the DocumentMetaData class I can select one or more documents from a drop down list (good!), and one or more Metadata from a dropdown list (good), and I can select one or more MetaDataValues from a drop down list (good). However, the bad news is that the relationship between the MetaData and MetaDataValue is not preserved.
For example, say I have MetaData 'people' with values John, Mark, Allison, and 'events' with values birthday, wedding, funeral. When I add a DocumentMetaData, I want to select 'people' from the MetaData dropdown, and then only the MetaDataValues for 'people' should be displayed in the MetaDataValues dropdown. Instead, what I get is all the MetaDataValues regardless of the associated MetaData, and I can add a MetaDataValue of John to a document under the Metadata label of 'events'. What am I missing in the relationship between the MetaDataValues class and the MetaData class?
These are my classes. I have removed some of the extraneous fields and methods in the classes for clarity.
class MetaData(models.Model):
metadata_id = models.AutoField(primary_key = True)
name = models.CharField('metadata name', max_length=200)
class MetaDataValue(models.Model):
metadata = models.ForeignKey(MetaData, on_delete=models.CASCADE,)
value = models.CharField('value', max_length=200)
class Document(models.Model):
document_id = models.AutoField(primary_key=True)
title = models.CharField('title', max_length=200)
class DocumentMetaData(models.Model):
document = models.ManyToManyField(Document)
metadata = models.ManyToManyField(MetaData)
metadatavalue = models.ManyToManyField(MetaDataValue)
Thanks!