Django: many-to-one fields and data integrity
Posted
by John
on Stack Overflow
See other posts from Stack Overflow
or by John
Published on 2010-05-03T22:21:17Z
Indexed on
2010/05/03
22:28 UTC
Read the original article
Hit count: 349
django
|django-models
|object-relational-mapping
|foreign-keys
|foreign-key-relationship
Let's say that I have a Person who runs an inventory system. Each Person has some Cars, and each Car has a very large number of Parts (thousands, let's say).
A Person, Bob, uses a Django form to create a Car. Now, Bob goes to create some Parts. It is only at the form level that Django knows that the Parts belong to some specific Car, and that the Parts.ForeignKey(Car) field should only have a specific Car as a choice. When creating a Part, you have to mess with the form's constructor or similar in order to limit the choice of Cars to only the cars owned by Bob.
It does not seem proper that to enforce this ownership at the form level. It seems that other users' Cars must be inaccessible to anyone but the owner of the Car. What do you all think about this, and is there any way to enforce this?
© Stack Overflow or respective owner