views:

26

answers:

1

Hi, I want to have a django model like this:

class Model(models.Model):
 string = models.CharField()
 class ContactDetails:
  phone = models.IntegerField()

Is this possible? I've tried googling but it doesn't seem to answer my question

This would mean i'd have while accesssing:

Model().ContactDetails.phone

working like that :)

Joe

+1  A: 

It can have embedded classes (a common case is class Meta), but any models.*Field members are ignored. It doesn't make sense in SQL.

What you want is a many-to-one:

class Thing(models.Model): # Don't name this class 'Model'!
    name = models.CharField(max_length=100)

class ContactDetails:
    parent = models.ForeignKey(Thing, related_name="contactDetails")
    phone = models.IntegerField()

Then, to access:

thing = Thing();
# ... set up thing ...
thing.save()
contact1 = ContactDetails(parent=thing)
# ... set up contact1 ...
contact1.save()
contact2 = ContactDetails(parent=thing)
# ... set up contact2 ...
contact2.save()
# ...
thing.contactDetails.all()
# returns a list with contact1 and contact2

or whatever.

Mike DeSimone
Thanks! :) That really helped!
Joe Simpson