Django

Code

Ticket #7240 (new)

Opened 2 months ago

Last modified 3 weeks ago

OneToOneField does not work with profiles in admin (oldforms)

Reported by: David Sauve <dnsauve@gmail.com> Assigned to: nobody
Milestone: Component: Admin interface
Version: SVN Keywords: OneToOneField Admin Profile
Cc: alexey.rudy@gmail.com Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Change History

06/15/08 08:18:50 changed by rudyryk <alexey.rudy@gmail.com>

  • cc set to alexey.rudy@gmail.com.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I have similar error.

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


Add/Change #7240 (OneToOneField does not work with profiles in admin (oldforms))




Change Properties
Action