[Django] #35050: Issue filtering on FilteredRelation with F object

33 views
Skip to first unread message

Django

unread,
Dec 18, 2023, 2:46:05 PM12/18/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------
Reporter: Mark Zorn | Owner: nobody
Type: Bug | Status: new
Component: | Version: 5.0
Uncategorized |
Severity: Release | Keywords: FilteredRelation
blocker |
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I just started trying to upgrade an existing project from Django 4.2.8 to
Django 5 and ran across the following issue.

I have two classes:

{{{
class DashboardSuggestion(BaseModel):

active = models.BooleanField(default=True)
title = models.CharField(max_length=50)
body = models.CharField(max_length=80)
max_dismissals = models.IntegerField(default=1)
days_between_dismissals = models.IntegerField(null=True, blank=True)

objects = DashboardSuggestionQuerySet.as_manager()

class Meta:
db_table = 'dashboard_suggestion'
}}}


{{{
class DashboardSuggestionDismiss(BaseModel):
suggestion = models.ForeignKey('suggestions.DashboardSuggestion',
on_delete=models.CASCADE, related_name='dismissals')
user = models.ForeignKey('users.User', on_delete=models.SET_NULL,
null=True, blank=True)
company = models.ForeignKey('companies.Company',
on_delete=models.SET_NULL, null=True, blank=True)
count = models.IntegerField(default=1)

class Meta:
db_table = 'dashboard_suggestion_dismiss'
unique_together = ['user', 'suggestion', 'company']
}}}

And the following QuerySet:

{{{
class DashboardSuggestionQuerySet(models.QuerySet):
def for_dashboard(self, user, company):
queryset = self.annotate(
is_dismissed=FilteredRelation(
'dismissals',
condition=(
Q(dismissals__user=user)
& Q(dismissals__company=company)
&
(
Q(dismissals__count__gte=F('max_dismissals'))
| Q(dismissals__updated__gt=timezone.now() -
timezone.timedelta(days=1) * F('days_between_dismissals'))
)
)
),
).filter(
Q(is_dismissed__isnull=True),
)
}}}

When I attempt to call `DashboardSuggestion.objects.for_dashboard(user,
company)` I receive the following error:

`Cannot resolve keyword 'days_between_is_dismissed' into field. Choices
are...`

The string `days_between_is_dismissed` does not exist anywhere in my code.
I do not see where it is being used or generated at all.

Trying to do a bit of debugging, somehow the
`F('days_between_dismissals')` in the FilteredRelation condition is
getting converted to `F(days_between_is_dismissed)` but I have not yet
found the exact place that is happening.

This code was working as I expected in Django 4.2.

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

Django

unread,
Dec 18, 2023, 2:57:11 PM12/18/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
----------------------------------+--------------------------------------
Reporter: mrzorn | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | 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 mrzorn:

Old description:

New description:

I have two classes:

{{{
class DashboardSuggestion(BaseModel):

objects = DashboardSuggestionQuerySet.as_manager()

And the following QuerySet:

Please let me know if I can supply any additional information.k

--

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

Django

unread,
Dec 18, 2023, 2:57:23 PM12/18/23
to django-...@googlegroups.com

Old description:

New description:

I have two classes:

{{{
class DashboardSuggestion(BaseModel):

objects = DashboardSuggestionQuerySet.as_manager()

And the following QuerySet:

--

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

Django

unread,
Dec 18, 2023, 3:19:52 PM12/18/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------

Reporter: mrzorn | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Francesco Panico (added)
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


Comment:

Thanks for the report!

Regression in 59f475470494ce5b8cbff816b1e5dafcbd10a3a3.

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

Django

unread,
Dec 22, 2023, 5:17:26 AM12/22/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------
Reporter: Mark Zorn | Owner: David
| Wobrock
Type: Bug | Status: assigned

Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

* cc: David Wobrock (added)
* owner: nobody => David Wobrock
* has_patch: 0 => 1
* status: new => assigned


Comment:

[https://github.com/django/django/pull/17634 PR]

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

Django

unread,
Dec 22, 2023, 8:07:34 AM12/22/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------
Reporter: Mark Zorn | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | 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 Mariusz Felisiak):

* stage: Accepted => Ready for checkin


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

Django

unread,
Dec 23, 2023, 12:32:52 PM12/23/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------
Reporter: Mark Zorn | Owner: David
| Wobrock
Type: Bug | Status: closed

Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: FilteredRelation | 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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"14917c9ae272f47d23401100faa6cefa8e1728bf" 14917c9a]:
{{{
#!CommitTicketReference repository=""
revision="14917c9ae272f47d23401100faa6cefa8e1728bf"
Fixed #35050 -- Fixed prefixing field names in FilteredRelation().

Thanks Mark Zorn for the report.

Regression in 59f475470494ce5b8cbff816b1e5dafcbd10a3a3.
}}}

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

Django

unread,
Dec 23, 2023, 12:33:53 PM12/23/23
to django-...@googlegroups.com
#35050: Issue filtering on FilteredRelation with F object
-------------------------------------+-------------------------------------
Reporter: Mark Zorn | Owner: David
| Wobrock
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: FilteredRelation | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9aad44150fa7632b894c8b1d1cb9c6dde131b577" 9aad441]:
{{{
#!CommitTicketReference repository=""
revision="9aad44150fa7632b894c8b1d1cb9c6dde131b577"
[5.0.x] Fixed #35050 -- Fixed prefixing field names in FilteredRelation().

Thanks Mark Zorn for the report.

Regression in 59f475470494ce5b8cbff816b1e5dafcbd10a3a3.

Backport of 14917c9ae272f47d23401100faa6cefa8e1728bf from main
}}}

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

Reply all
Reply to author
Forward
0 new messages