[ Django modelling - using the right relationships ]
I want a model 'Model' to have a set (zero or more) of 'Attribute's. It should be able to access these 'Attribute's. A single 'Attribute' has exactly ONE 'Model'.
A 'Model' has zero or more 'Objects'. It should be able to access these 'Object's. A single 'Object' has exactly ONE 'Model'. An 'Object' will inherit all the 'Attribute's of it's 'Model'.
I'm not sure how to create these models in Django.
Here is what I have so far:
class Model(models.Model):
#
class Attribute(models.Model):
model = models.ForeignKey(Model)
class Object(models.Model):
model = models.ForeignKey(Model)
UPDATE
When I try to make an 'Object' object, I get this error:
column myproject_object.model_id does not exist
Similarly, if I try to make a 'Model' object. I get this error:
null value in column "attributes_id" violates not-null constraint
Answer 1
This relation is called Many-to-many relationship and Django has implemented it in its ORM.
class Child(models.Model):
# [...]
class Parent(models.Model):
children = models.ManyToManyField(Model)
You can access in this way
parent = Parent()
parent.save()
parent.children.all()
Django create automatically the relation in database, I highly recommend you to read the Django documentation about Many-to-many relation.