[Django] #28222: Django 1.11 update_or_create field validation results in different behaviours between create and update_or_create

21 views
Skip to first unread message

Django

unread,
May 18, 2017, 11:07:54 PM5/18/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-----------------------------------------+------------------------
Reporter: Alex Mykyta | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords: 1.11
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
This is an issue related to #27118. Anthony King commented on it but that
issue is closed so I am opening a new one. The field validation introduced
in 1.11 `update_or_create` will throw a FieldError if a `defaults`
argument contains a value that is set through an `@property.setter` on
that model. On the other hand `create` continues to function correctly.
Ideally they'd behave consistently.

Here is an example https://dpaste.de/UORC

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

Django

unread,
May 18, 2017, 11:10:30 PM5/18/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------+--------------------------------------

Reporter: Alex Mykyta | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: 1.11 | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Alex Mykyta:

Old description:

> This is an issue related to #27118. Anthony King commented on it but that
> issue is closed so I am opening a new one. The field validation
> introduced in 1.11 `update_or_create` will throw a FieldError if a
> `defaults` argument contains a value that is set through an
> `@property.setter` on that model. On the other hand `create` continues to
> function correctly. Ideally they'd behave consistently.
>
> Here is an example https://dpaste.de/UORC

New description:

This is an issue related to #27118. Anthony King commented on it but that
issue is closed so I am opening a new one. The field validation introduced
in 1.11 `update_or_create` will throw a `FieldError` if a `defaults`
argument contains a value that is set through an `@property.setter` on
that model. On the other hand `create` continues to function correctly.
Ideally they'd behave consistently.

Here is an example https://dpaste.de/UORC

--

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

Django

unread,
May 18, 2017, 11:38:58 PM5/18/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------------+-------------------------------------

Reporter: Alex Mykyta | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:

Keywords: 1.11 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Mykyta):

* component: Uncategorized => Database layer (models, ORM)


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

Django

unread,
May 19, 2017, 1:11:57 AM5/19/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: assigned

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 1.11 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Mykyta):

* owner: nobody => Alex Mykyta
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:3>

Django

unread,
May 19, 2017, 7:30:34 AM5/19/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 1.11 | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Mykyta):

* has_patch: 0 => 1
* stage: Unreviewed => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:4>

Django

unread,
May 19, 2017, 7:43:07 AM5/19/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 1.11 | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Ready for checkin => Accepted


Comment:

Please move the pastebin content (which expires in 1 day) to the ticket's
description.

The "Ready for checkin" status is set by a patch reviewer, not the patch
author.

--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:5>

Django

unread,
May 19, 2017, 11:53:31 AM5/19/17
to django-...@googlegroups.com
#28222: Django 1.11 update_or_create field validation results in different
behaviours between create and update_or_create
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 1.11 | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Old description:

> This is an issue related to #27118. Anthony King commented on it but that
> issue is closed so I am opening a new one. The field validation
> introduced in 1.11 `update_or_create` will throw a `FieldError` if a
> `defaults` argument contains a value that is set through an
> `@property.setter` on that model. On the other hand `create` continues to
> function correctly. Ideally they'd behave consistently.
>
> Here is an example https://dpaste.de/UORC

New description:

This is an issue related to #27118. Anthony King commented on it but that
issue is closed so I am opening a new one. The field validation introduced
in 1.11 `update_or_create` will throw a `FieldError` if a `defaults`
argument contains a value that is set through an `@property.setter` on
that model. On the other hand `create` continues to function correctly.
Ideally they'd behave consistently.

Here is an example.
{{{
# create works, update_or_create does not. Should neither work?
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

_credit = models.DecimalField('Credit', decimal_places = 5,
max_digits=11, null=True)

@property
def credit(self):
return 0 if self._credit is None else self._credit

@credit.setter
def credit(self, value):
self._credit = value


In [7]: mk = Choice.objects.create(credit=123.3, poll_id=1)

In [8]: mk
Out[8]: <Choice: >

In [9]: mk.__dict__
Out[9]:
{'_credit': 123.3,
'_state': <django.db.models.base.ModelState at 0x1116e3650>,
'choice_text': u'',
'id': 2,
'poll_id': 1,
'votes': 0}

In [10]: ok = Choice.objects.update_or_create(defaults={'credit':123.3},
poll_id=1)
---------------------------------------------------------------------------
FieldError Traceback (most recent call
last)
<ipython-input-10-147ffa08785c> in <module>()
----> 1 ok = Choice.objects.update_or_create(defaults={'credit':123.3},
poll_id=1)

/usr/local/lib/python2.7/site-packages/django/db/models/manager.pyc in
manager_method(self, *args, **kwargs)
83 def create_method(name, method):
84 def manager_method(self, *args, **kwargs):
---> 85 return getattr(self.get_queryset(), name)(*args,
**kwargs)
86 manager_method.__name__ = method.__name__
87 manager_method.__doc__ = method.__doc__

/usr/local/lib/python2.7/site-packages/django/db/models/query.pyc in
update_or_create(self, defaults, **kwargs)
474 """
475 defaults = defaults or {}
--> 476 lookup, params = self._extract_model_params(defaults,
**kwargs)
477 self._for_write = True
478 with transaction.atomic(using=self.db):

/usr/local/lib/python2.7/site-packages/django/db/models/query.pyc in
_extract_model_params(self, defaults, **kwargs)
530 "Invalid field name(s) for model %s: '%s'." % (
531 self.model._meta.object_name,
--> 532 "', '".join(sorted(invalid_params)),
533 ))
534 return lookup, params

FieldError: Invalid field name(s) for model Choice: 'credit'.
}}}

--

Comment (by Alex Mykyta):

Whoops. Sorry, still figuring out the contribution process.

--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:6>

Django

unread,
May 26, 2017, 8:23:33 PM5/26/17
to django-...@googlegroups.com
#28222: QuerySet.update_or_create() field validation should allow settable
properties in defaults

-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: 1.11 | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

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

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:7>

Django

unread,
May 27, 2017, 1:01:37 PM5/27/17
to django-...@googlegroups.com
#28222: QuerySet.update_or_create() field validation should allow settable
properties in defaults
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: 1.11 | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"37ab3c3f9d707d6a1896db79c631e920dcb1fb78" 37ab3c3f]:
{{{
#!CommitTicketReference repository=""
revision="37ab3c3f9d707d6a1896db79c631e920dcb1fb78"
Fixed #28222 -- Allowed settable properties in
QuerySet.update_or_create()/get_or_create() defaults.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:8>

Django

unread,
May 27, 2017, 1:08:09 PM5/27/17
to django-...@googlegroups.com
#28222: QuerySet.update_or_create() field validation should allow settable
properties in defaults
-------------------------------------+-------------------------------------
Reporter: Alex Mykyta | Owner: Alex
| Mykyta
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: 1.11 | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"b9abdd92ab21a796943e8047e778eebc671c8c00" b9abdd92]:
{{{
#!CommitTicketReference repository=""
revision="b9abdd92ab21a796943e8047e778eebc671c8c00"
[1.11.x] Fixed #28222 -- Allowed settable properties in
QuerySet.update_or_create()/get_or_create() defaults.

Backport of 37ab3c3f9d707d6a1896db79c631e920dcb1fb78 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28222#comment:9>

Reply all
Reply to author
Forward
0 new messages