I’m trying to write an inline editor for a specific type I’m writing. My types look like this:
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Donor(models.Model):
# a donor _is_ a person
person = models.OneToOneField("Person")
The goal is to be able to edit the information stored in Person from a Donor, as Person is more of an abstract type (though not entirely). Here’s my admin:
class PersonInline(admin.StackedInline):
model = Person
fields = ('first_name', 'last_name')
class DonorAdmin(admin.ModelAdmin):
inlines = (PersonInline,)
admin.site.register(Donor, DonorAdmin)
Unfortunately, when I go to create a new Donor in the admin site, I see the following error:
Exception at /core/donor/add/
<class 'core.models.Person'> has no ForeignKey to <class 'core.models.Donor'>
Odd. Why isn’t it working with my very simple inline?
EDIT
I’ve found that it works if I change the location of the references to be on the Person class rather than on the Donor, but this really fouls up my design:
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
donor = models.OneToOneField("Donor", blank=True, null=True)
recipient = models.OneToOneField("Recipient", blank=True, null=True)
recipient_dependant = models.OneToOneField("RecipientDependant", blank=True, null=True)
class Donor(models.Model):
pass
class Recipient(models.Model):
pass
class RecipientDependant(models.Model):
pass
This seems kind of backwards to me, because a Donor is a/owns a Person object, but a Person is not/does not own a Donor. Plus, this is a bit crazy. The Person class doesn’t need to know necessarily what owns it at all.
You can’t have an inline for Person, but have the relationship owner (where the OneToOne field is) be the donor.
In your case, you would need a DonorInline and a PersonAdmin.