[Django] #24691: UUIDField sets value before save

9 views
Skip to first unread message

Django

unread,
Apr 23, 2015, 5:03:04 AM4/23/15
to django-...@googlegroups.com
#24691: UUIDField sets value before save
-------------------------------+----------------------------
Reporter: MattBlack85 | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Keywords: uuid UUIDField
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------
When using a pk I expect the behaviour we had in the past meaning that
inside .save() method checking for self.pk gives me a clue of creation vs
update (documentation confirms that
https://docs.djangoproject.com/en/1.8/ref/models/instances/#how-django-
knows-to-update-vs-insert). This is not working when using UUIDField since
when we enter the save method the pk is already set to the default value.

to reproduce
{{{
import uuid

from django.db import models

class AwesomeModel(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
x = models.CharField(max_length=10)

def save(self, *args, **kwargs):
if self.pk:
print("we have already a pk set")
return super(AwesomeModel, self).save(*args, **kwargs)


a = AwesomeModel(x='some')
a.save()
we have already a pk set
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24691>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 23, 2015, 10:21:35 AM4/23/15
to django-...@googlegroups.com
#24691: Document model._state.adding (since UUIDField sets value before save)
--------------------------------------+------------------------------------
Reporter: MattBlack85 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: uuid UUIDField | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* component: Uncategorized => Documentation
* needs_tests: => 0
* needs_docs: => 0
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

We should likely document `model._state.adding`. See #24377 for a patch
where we had to adapt the admin.

--
Ticket URL: <https://code.djangoproject.com/ticket/24691#comment:1>

Django

unread,
Apr 22, 2020, 2:44:02 PM4/22/20
to django-...@googlegroups.com
#24691: Document model._state.adding (since UUIDField sets value before save)
-------------------------------------+-------------------------------------
Reporter: Mattia Procopio | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: duplicate

Keywords: uuid UUIDField | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #31502.

--
Ticket URL: <https://code.djangoproject.com/ticket/24691#comment:2>

Reply all
Reply to author
Forward
0 new messages