Code

Opened 4 years ago

Closed 3 years ago

#14029 closed Bug (duplicate)

not operator on F objects silently fails

Reported by: ramiro Owned by: marcosmoyano
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords:
Cc: amusikal Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

[On behalf of Matías Bellone aka toote that got his submission rejected as SPAM]

Trying to make a toggle action in the admin on a BooleanField lead me to very simple and straightforward code inside the ModelAdmin subclass. Problem is that it didn't work and no error was presented whatsoever. Even worse, the update actually executes something that modifies the queryset but I haven't found a way to identify how to get the query executed.

The model can be just as simple as:

from django.db.models import Model, BooleanField
 
class TestModel(Model):
    field1 = BooleanField()

Testing the "action" in an interactive shell has the same results:

$ ./manage.py shell
Python 2.6.5+ (release26-maint, Jul  6 2010, 12:58:20)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from models import TestModel
>>> a = TestModel.objects.all()
>>> for instance in a:
...     print instance.field1
...
True
True
>>> from django.db.models import F
>>> a = TestModel.objects.all()
>>> # to return to having a non-executed queryset
>>> a.update(field1=not F('field1'))
2
>>> a = TestModel.objects.all()
>>> for instance in a:
...     print instance.field1
...
True
True


Further tests with the debug_toolbar shows that no matter what the original values of field1 is the update will set it to true (tested on querysets with 1 and 2 instances with all possible combinations of field1 values).

Attachments (0)

Change History (8)

comment:1 Changed 4 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Probably __nonzero__ should just raise an exception, and we should add invert to F().

comment:2 Changed 3 years ago by dmoisset

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by marcosmoyano

  • Owner changed from nobody to marcosmoyano
  • Status changed from new to assigned

comment:4 Changed 3 years ago by marcosmoyano

I think a query like field=~Q('field') wouldn't be valid (or usefull) since it will query items with a negated value than the actual value, this giving an empty result all the time.
ie:

SELECT "ticket_14029_testmodel"."id", "ticket_14029_testmodel"."field1" FROM "ticket_14029_testmodel" WHERE NOT "ticket_14029_testmodel"."field1" = "ticket_14029_testmodel"."field1" LIMIT 21;

comment:5 Changed 3 years ago by marcosmoyano

s/Q/F/

comment:6 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 3 years ago by amusikal

  • Cc amusikal added
  • Easy pickings unset
  • UI/UX unset

comment:8 Changed 3 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from assigned to closed

#16211 asked for the same feature, and altough it is newer it has a patch. So I'm going to close this one.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.