#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!
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)
follow-up: 3 comment:1 by , 4 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
comment:2 by , 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.
follow-up: 4 comment:3 by , 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?
follow-up: 5 comment:4 by , 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.
comment:5 by , 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 :)
Duplicate of #18638. Please don't open duplicates of existing tickets and follow triaging guidelines with regards to closed tickets.