Question on Django ORM

0 views
Skip to first unread message

devdiscuss

unread,
Mar 13, 2007, 2:41:11 PM3/13/07
to Django users
Hi, does the ORM in Django support nested objects? For instance,
assuming I have a User model which should have multiple mailing
addresses, can I encapsulate the address information (street, city,
state, etc.) into an object vs. individual fields like
address1_street, address1_city? In looking through the documentation,
nothing jumped out at me. Apologies if I have overlooked something
obvious here.

Thanks!

Condredge

unread,
Mar 13, 2007, 2:51:42 PM3/13/07
to Django users
Yep, you can use a Related Model. So you might do it this way:

class Address(models.Model):
line1 = models.Charfield(maxlength=100)
line2 = models.Charfield(maxlength=100, blank=True, null=True)
city = models.Charfield(maxlength=100)
state = models.Charfield(maxlength=2)
country = models.Charfield(maxlength=100)
zip = models.Charfield(maxlength=20)

class UseProfile(models.Model):
shipping_address = models.OneToOneField(Address)
billing_address = models.OneToOneField(Address)


Check out: http://www.djangoproject.com/documentation/models/one_to_one/
for more info on using OneToOne fields.

James Bennett

unread,
Mar 13, 2007, 6:06:49 PM3/13/07
to django...@googlegroups.com

If you want it to be encapsulated in a class, define the class
separately and relate it back to the class it pertains to, say, with a
unique foreign key.

If you just want to control getting/setting, use a property:

http://www.djangoproject.com/documentation/models/properties/

--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages