I’d like to know the best way to associate various data types with an object in Django. Some of the types should be string, boolean, image file, choice from a list, or a link. For example, say you have a Product model. For product X, you’ll want to add an image attribute, a string for the model name, and a link. For product Y, possible attributes would be an image, a weight decimal. What would be the best way to set this up? Are there any packages available that do this or something similar?
Share
You can either make a single model that allows for blank/null values for each field. Or use django’s model inheritance if you are setting up types of products that share similar desired attributes or fall into categories. It seems like you are asking for optional attributes which would just require you to define optional fields in the model (first example).
null
ref, blankrefWithout Inheritance:
Inheritance
ref:edit:
read about relationships in the
docsanddocsThe ProductXXXXX are related to the Product model by a foreign key. It is a one to many relationships, so for each product you can have many productxxxx.
As an example, you can make a product:
Now you want to add a weight to that product:
To see all of the weights related to the product:
This allows you to have products with different “attributes”.