Model / Inline / Admin questions

6 views
Skip to first unread message

TehOne

unread,
Aug 7, 2008, 5:58:56 PM8/7/08
to Django users
Ok, so a quick summary.

I have an address model. I created this so that I can use standard
address' everywhere that i need them. So, for example, an Account can
have a primary and a secondary address. And, a Contact can have an
address. So, instead of duplicating the fields i needed in all my
models, i just created a model for Address. So, what I want to do, i
have it so that when editing/creating a Contact or an Account, I can
just edit the address inline in the admin pages. I originally tried
this using a ForeignKey, but have most recently tried this with a
OneToOneField. So far though, I haven't been able to get this working
and have been getting a lot of errors whenever I try to get any
inlining working for this. For example, here is one error
"`PrimaryAddressInline.fk_name` refers to field
`account_primary_address` that is missing from model `Address`". I
have tried a few different ways of doing the inline, but so far
everything leads me to an error.

Anyone have any ideas how I would get something like this working?

class Contact(models.Model):
"""(Contact description)"""
user = models.ForeignKey(User)
main_phone = models.PhoneNumberField(blank=True)
secondary_phone = models.PhoneNumberField(blank=True)
fax_phone = models.PhoneNumberField(blank=True)
#primary_address = models.ForeignKey(Address,
related_name="contact_primary_address", unique=True)
#secondary_address = models.ForeignKey(Address,
related_name="contact_secondary_address", unique=True)
primary_address = models.OneToOneField(Address,
related_name="contact_primary_address")
secondary_address = models.OneToOneField(Address,
related_name="contact_secondary_address")

notes = NotesField()

def __unicode__(self):
return "%s %s" % (self.user.first_name, self.user.last_name)


class Address(models.Model):
"""(Address description)"""
address1 = models.CharField(max_length=100)
address2 = models.CharField(max_length=100)
city = models.CharField(max_length=100)
state = models.CharField(max_length=25)
postal_code = models.CharField(max_length=15)
country = models.CharField(max_length=50)

def __unicode__(self):
return "Address"

class Account(models.Model):
"""(Account description)"""
#primary_address = models.ForeignKey(Address,
related_name="account_primary_address", unique=True)
#secondary_address = models.ForeignKey(Address,
related_name="account_secondary_address", unique=True)
primary_address = models.OneToOneField(Address,
related_name="account_primary_address")
secondary_address = models.OneToOneField(Address,
related_name="account_secondary_address")
name = models.CharField(max_length=100)
notes = NotesField()

def __unicode__(self):
return self.name

class PrimaryAddressInline(admin.TabularInline):
model = Address
fk_name = "account_primary_address"

class AccountAdmin(admin.ModelAdmin):
#pass
#fields = ['']
#fieldsets = [
# (None, {'fields': ['']}),
# ('Fieldset Name', {'fields': ['']},
#]
inlines = [PrimaryAddressInline,]
#list_display = ('',)
#list_filter = ['']
#search_fields = ['']

admin.site.register(Account, AccountAdmin)
Reply all
Reply to author
Forward
0 new messages