Django OneToOneField and admin UI

I'm getting confused about how to model a relationship in Django so that it can be edited inline in the Admin Admin.

Let me explain the scenario.

I have a client model and an address model. In the customer model, I have a OneToOneField relationship to address once for billing and once for shipping address.

class Address(models.Model):
    pass

class Employee(models.Model):
    billing_address = models.OneToOneField(Address)
    shipping_address = models.OneToOneField(Address)
    # Many more such fields

      

Now with this model, there is no easy way to make them embedded in Admin. I have tried the following

class AddressInline(admin.TabularInline):
    model = Address

class Customer(admin.ModelAdmin):
    inlines = [AddressInline, ]

      

I am getting the error,

<class 'employee.admin.AddressInline'>: (admin.E202) 'employee.Address' has no ForeignKey to 'employee.Customer'.

      

Now I know there are other errors similar to this one. i.e. Use OneToOneField inline in Django Admin and Django admin - OneToOneField inline throws "has no ForeignKey" Exception

But I think my question is slightly different to justify this post. Please, help!

+3


source to share





All Articles