Opened 16 years ago

Closed 16 years ago

#7240 closed (wontfix)

OneToOneField does not work with profiles in admin (oldforms)

Reported by: David Sauve <dnsauve@…> Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: OneToOneField Admin Profile
Cc: alexey.rudy@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (4)

comment:1 by rudyryk <alexey.rudy@…>, 16 years ago

Cc: alexey.rudy@… added

I have similar error.

Is I see, ANY Model with OneToOne field can NOT be edited with admin interface due to this.

comment:2 by tobi, 16 years ago

I had the same problem. Adding "editable=True" as parameter to the field solved the problem!

in reply to:  2 comment:3 by tobi, 16 years ago

Replying to tobi:

I had the same problem. Adding "editable=True" as parameter to the field solved the problem!

Ok. Does not work...

comment:4 by Julian Bez, 16 years ago

Resolution: wontfix
Status: newclosed

Oldforms is history...

Note: See TracTickets for help on using tickets.
Back to Top