Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32056 closed New feature (duplicate)

Improve assigning to the reverse side of a OneToOne relationship

Reported by: Yu Li Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello, Django 3.1 user here.

I find the fact that you cannot save by assigning to a reverse one-to-one relationship counter-intuitive.
Obviously this has confused other users, see related:

https://code.djangoproject.com/ticket/18638
https://code.djangoproject.com/ticket/18638
https://stackoverflow.com/questions/43119575/automatically-create-one-to-one-relation-when-using-reverse-field

I think when you do

data = Data()
t = Tag.objects.create()
data.tag = t
data.save()
# refresh from db
data = Data.objects.get(pk=data.pk)
data.tag # raises DoesNotExist

You should be able to save the relation by calling .save() on the data object. Since ReverseOneToOneDescriptor of .tag has access to data fom its __set__(self, instance, value) method, I believe it is possible to save some information on data and later can be used to save the relation when calling data.save().

An even worse behavior is that when creating an object using the reverse relation, the creation fails silently. For instance,

t = Tag.objects.create()
data = Data.objects.create(tag=t)
data.tag // does not exists!

See https://stackoverflow.com/questions/43119575/automatically-create-one-to-one-relation-when-using-reverse-field

Similarly, I don't see how this can't be achieved. The fact that the code doesn't work is counter-intuitive.
At least, If this kind of behavior is unsupported by design, I believe the create method should at least throw an error.

Change History (5)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed

Duplicate of #18638. Please don't open duplicates of existing tickets and follow triaging guidelines with regards to closed tickets.

comment:2 by Yu Li, 4 years ago

Hello, this was a feature request. If this couldn't be done because of an architectural reason, please suggest here so that other users can find an explanation.

in reply to:  1 ; comment:3 by Yu Li, 4 years ago

Replying to felixxm:

Duplicate of #18638. Please don't open duplicates of existing tickets and follow triaging guidelines with regards to closed tickets.

I'm not sure why you are closing this as a duplicate when the other ticket is also closed.
Is this a wontfix?

in reply to:  3 ; comment:4 by Mariusz Felisiak, 4 years ago

I'm not sure why you are closing this as a duplicate when the other ticket is also closed.
Is this a wontfix?

There is no reason to open multiple tickets with the same description and the same request. You can comment in the original ticket instead of creating duplicates. If you don't agree with the decision made in #18638 please follow triaging guidelines with regards to wontfix tickets.

in reply to:  4 comment:5 by Yu Li, 4 years ago

Replying to felixxm:

I'm not sure why you are closing this as a duplicate when the other ticket is also closed.
Is this a wontfix?

There is no reason to open multiple tickets with the same description and the same request. You can comment in the original ticket instead of creating duplicates. If you don't agree with the decision made in #18638 please follow triaging guidelines with regards to wontfix tickets.

Thanks for pointing out the direction :)

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