Code

Opened 6 years ago

Closed 6 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: master
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: UI/UX:

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.

Attachments (0)

Change History (4)

comment:1 Changed 6 years ago by rudyryk <alexey.rudy@…>

  • Cc alexey.rudy@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I have similar error.

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

comment:2 follow-up: Changed 6 years ago by tobi

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

comment:3 in reply to: ↑ 2 Changed 6 years ago by tobi

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 Changed 6 years ago by julianb

  • Resolution set to wontfix
  • Status changed from new to closed

Oldforms is history...

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.