I have a template in Django with fields largura
and comprimento
, and I would like to filter it by area:
class Projeto(models.Model):
largura = models.DecimalField(decimal_places=0, max_digits=4, default=0)
comprimento = models.DecimalField(decimal_places=0, max_digits=4, default=0)
I tried the following query:
Projeto.objects.annotate(area=F('largura')*F('comprimento')).filter(area__lte=1000)
However, it did not work: the query returned all lines of Projeto
, even those that have an area greater than 1000
. What am I doing wrong?
I checked the SQL that was being generated by the query (formatted for readability):
>>> print Projeto.objects.annotate(area=F('largura')*F('comprimento')).filter(area__lte=1000).query
SELECT "mcve_projeto"."id", "mcve_projeto"."largura", "mcve_projeto"."comprimento",
("mcve_projeto"."largura" * "mcve_projeto"."comprimento") AS "area"
FROM "mcve_projeto"
WHERE ("mcve_projeto"."largura" * "mcve_projeto"."comprimento") <= 1000
as well as the database creation SQL (idem):
python manage.py sqlmigrate mcve 0001
BEGIN;
CREATE TABLE "mcve_projeto" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"largura" decimal NOT NULL,
"comprimento" decimal NOT NULL);
COMMIT;
and I created an example in SQLFiddle (note: I'm using SQLite). It worked normal. Where might the problem be?