When defining a user field as a OneToOneField? (instead of the recommended ForeignKey?(unique=True)) in a Profile object, the object fails validation when editing in the admin interface.
Here's an example model:
class Profile(models.Model):
"""
Extra user profile information.
"""
user = models.OneToOneField(User, verbose_name=_('user'))
icon = models.ImageField(_('icon'), upload_to='profiles', null=True, blank=True)
address = models.CharField(_('address'), max_length=200, blank=True)
city = models.CharField(_('city'), max_length=50, blank=True)
state_province = models.CharField(_('state/province'), max_length=50, blank=True)
postal_code = models.CharField(_('postal code/zip code'), max_length=10, blank=True)
country = models.CharField(_('country'), max_length=50, blank=True)
class Meta:
verbose_name = _('profile')
verbose_name_plural = _('profiles')
class Admin:
pass
def __unicode__(self):
return "%s, %s" % (self.user.last_name, self.user.first_name)
@permalink
def get_absolute_url(self):
return ('profiles-details', (), { 'username': self.user.username })
When using this model, I can create new users and profiles in the admin interface without any issues, however, when trying to edit a instance of a profile, it fails validation with the error: "This field is required." and the "User" field highlighted.
There is no problem when using the same model, above, to create/edit instances of the profile in the shell:
>>>u = User.objects.get(username='David')
>>>p = u.get_profile()
>>>p.address = '123 Main St.'
>>>p.save()
This was tested using trunk 0.97-pre-SVN-7534 on WindowsXP and SQLite3.