On May 10, 4:03 pm, Tom von Schwerdtner <tomv...@gmail.com> wrote:
> Greetings folks, I've been wrestling with this and I really don't know
> what I'm not doing right, so here is where I am:
>
> In basic pseudocode, I have:
>
> models.py:
> ---------------
> class Address(models.Model):
>     ... ( street, city, etc)
>
> class Contact(models.Model):
>     ...
>     address = models.OneToOneField('Address')
>
> admin.py:
> -------------
>
> class AddressInline(admin.StackedInline):
>     model = models.Address
>
> class ContactAdmin(admin.ModelAdmin):
>     inlines = [AddressInline,]
>
> ################
>
> However, when I try to add a new Contact I get this error:
>
> <class 'myapp.models.Address'> has no ForeignKey to <class
> 'myapp.models.Contact'>
>
> It seems like this should work, any ideas what I'm doing wrong or what
> the correct way to handle this is?
>
> Thanks,
> Tom

Inlines are for objects that have connected objects which are not
directly related. Your contact model is directly related to address
because it has an "address" field. If the relationship was via a
"contact" field on the address model, then you'd need an inline.

In your example Django assumes the address model has a foreign key
relating to contact and it tries to set that foreign key to the
address pk, but it's not there so it errors.

My suggest for you is to just get rid of the inline all together. It
should automatically create a little widget with a green plus sign for
creating a new address object. If it's a no-popup inline-esque widget
you're after, then there may be another way to do that but I don't
know how.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to