Problem with error admin.E202 on OneToOneRelation

258 views
Skip to first unread message

Richard la Croix

unread,
Apr 8, 2015, 9:04:33 AM4/8/15
to django...@googlegroups.com
Hello,

I have problems with a OneToOneRelation in this simple model.
Without Inline in the admin.py it basically works, but when I add an Inline for the Address to the Resort, I get error messages like:

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

I think that I followed the examples from the tutorial quite closely and that a foreign key definition on the Address 'returning' to the Resort should not be required. Other addresses might/will be owned by other objects/classes.

Obviously I must be doing something wrong :-( Any suggestions?

Thanks, Richard


from django.db import models
from django.utils.translation import ugettext_lazy as _

class Address(models.Model):
number = models.PositiveIntegerField(_('Adress Nr'))
function = models.CharField(_('Adressfunktion'), max_length=2)
name = models.CharField(_('Name'), max_length=64)
street = models.CharField(_('Strasse und Hausnummer'), max_length=64)
city = models.CharField(_('Ort'), max_length=64)
postal_code = models.CharField(_('PLZ'), max_length=10)
phone = models.CharField(_('Telefon'), max_length=32)
fax = models.CharField(_('Fax'), max_length=32)

def __str__(self):
return '%s (%i)' % (self.name, self.number)

class Meta:
unique_together = ('number', 'function')
ordering = ['number', 'function']
verbose_name = _('Adresse')
verbose_name_plural = _('Adressen')


class Resort(models.Model):
base_address = models.OneToOneField(Address, verbose_name=_('Resortadresse'), primary_key=True)
description = models.CharField(_('Bezeichnung'), max_length=64)
short_description = models.CharField(_('Kurzbezeichnung'), max_length=20)
sihot_nr = models.PositiveSmallIntegerField(_('Sihot Nr'), unique=True)
resort_type = models.CharField(_('Resorttype'), max_length=1)


Admin:
from django.contrib import admin
from testApp.models import Address, Resort

class AddressInline(admin.StackedInline):
model = Address
extra = 1

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

@admin.register(Resort)
class ResortAdmin(admin.ModelAdmin):
inlines = [AddressInline]

If I register the Address and the Resort class, it gets worse:
<class 'testApp.admin.AddressInline'>: (admin.E202) 'testApp.Address' has no ForeignKey to 'testApp.Address'.
<class 'testApp.admin.AddressInline'>: (admin.E202) 'testApp.Address' has no ForeignKey to 'testApp.Resort'.

Reply all
Reply to author
Forward
0 new messages