Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#10157 closed (fixed)

Assignment to a reverse OneToOne overwrites related object's PK

Reported by: Pavel Anossov Owned by: ianschenck
Component: Database layer (models, ORM) Version: master
Severity: Keywords: onetoone
Cc: dgouldin@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

example models:

class User(models.Model):
    name = models.CharField(max_length=10)
    def __unicode__(self):
        return '<USER: %s>' % self.name
        
class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile', null=True)


When I try to assign a UserProfile instance to a User instance's “profile” attribute, UserProfile's PK AutoField gets overwritten with the User instance:

>>> u = User(name='test')
>>> p = UserProfile()
>>> print p.id, p.user
None None
>>> u.profile = p
>>> print p.id, p.user
<USER test> <USER test>
# Expected: None <USER test>

Attachments (2)

10157.diff (920 bytes) - added by David Gouldin 8 years ago.
10157_2.diff (1.3 KB) - added by David Gouldin 8 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 8 years ago by Jacob

milestone: 1.1
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by ianschenck

Owner: changed from nobody to ianschenck

Changed 8 years ago by David Gouldin

Attachment: 10157.diff added

comment:3 Changed 8 years ago by David Gouldin

Cc: dgouldin@… added
Has patch: set

comment:4 Changed 8 years ago by Jacob

Triage Stage: AcceptedReady for checkin

comment:5 Changed 8 years ago by Jacob

Triage Stage: Ready for checkinAccepted

Oh, wait, needs a test.

Changed 8 years ago by David Gouldin

Attachment: 10157_2.diff added

comment:6 Changed 8 years ago by Jacob

Triage Stage: AcceptedReady for checkin

comment:7 Changed 8 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [10224]) Fixed #10157: correctly set the related objects pk when assigning a reverse OneToOne. Thanks, dgouldin.

comment:8 Changed 8 years ago by Jacob

(In [10291]) [1.0.X] Fixed #10157: correctly set the related objects pk when assigning a reverse OneToOne. Thanks, dgouldin.

Backport of r10224 from trunk.

comment:9 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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