Unsure how to correctly use Django models for a relationship with extra info
I have clients and contacts, and each client can have any number of contacts and each contact can have any number of types. For example, client 1 can have two billing contacts, person A and person B, and two business contacts, person B and person C.
I know it would be possible to model this with the开发者_如何学编程 following models:class Client(models.Model):
id #primary key
#other data
class Contact(models.Model):
id #primary key
#other data
class Relationship(models.Model):
client_id=ForeignKey(Client)
contact_id=ForeignKey(Contact)
type=CharField(max_length=255)#or some other field that represents the type
but this seems incorrect to me because the Relationship model does not represent an object but a relation between other objects. Do I need to do it this way, or is there some way of making the models so that every one actually represents the object
but this seems incorrect to me because the Relationship model does not represent an object but a relation between other objects.
A relationship is a first-class thing.
In a simple RDBMS models, the relationship was implied by a shared key (FK in one, PK in another)
However, when you have many-to-many association tables, you create an explicit row which embodies the relationship.
You're just enriching the association object with additional attributes.
This is fine. It's common, in fact.
It's a generalization of this: https://docs.djangoproject.com/en/1.3/topics/db/models/#many-to-many-relationships
Also, read this: https://docs.djangoproject.com/en/1.3/topics/db/models/#extra-fields-on-many-to-many-relationships
精彩评论