Sunday, June 25, 2017

Model design question

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!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2PO2bMW%2BZ%3DPJVy66%3DWspm6E7zetGrYHyxjg-9iOjBmzaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment