Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GeneratedFields #1688

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
)
from .utils import get_all_model_fields, get_model_field, resolve_field, try_dbfield

try:
from django.db.models import GeneratedField
except ImportError:
DJANGO_50 = False
else:
DJANGO_50 = True


def remote_queryset(field):
"""
Expand Down Expand Up @@ -390,6 +397,13 @@ def handle_unrecognized_field(cls, field_name, message):
def filter_for_field(cls, field, field_name, lookup_expr=None):
if lookup_expr is None:
lookup_expr = settings.DEFAULT_LOOKUP_EXPR

# Handle GeneratedFields
if DJANGO_50 and isinstance(field, GeneratedField):
new_field = field.output_field
new_field.model = field.model
field = new_field

field, lookup_type = resolve_field(field, lookup_expr)

default = {
Expand Down
19 changes: 19 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from django import forms
from django.db import models
from django.db.models import F
from django.utils.translation import gettext_lazy as _

try:
from django.db.models import GeneratedField
except ImportError:
DJANGO_50 = False
else:
DJANGO_50 = True

REGULAR = 0
MANAGER = 1
ADMIN = 2
Expand Down Expand Up @@ -210,3 +218,14 @@ class SpacewalkRecord(models.Model):

astronaut = models.CharField(max_length=100)
duration = models.DurationField()


if DJANGO_50:
class Rectangle(models.Model):
base = models.FloatField()
height = models.FloatField()
area = GeneratedField(
expression=F("base") * F("height"),
output_field=models.FloatField(),
db_persist=True,
)
23 changes: 17 additions & 6 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
)
from .utils import MockQuerySet

try:
from .models import Rectangle
except ImportError:
DJANGO_50 = False
else:
DJANGO_50 = True


class HelperMethodsTests(TestCase):
@unittest.skip("todo")
Expand Down Expand Up @@ -119,6 +126,14 @@ def test_filter_found_for_uuidfield(self):
self.assertIsInstance(result, UUIDFilter)
self.assertEqual(result.field_name, "uuid")

def test_filter_found_for_generatedfield(self):
if not DJANGO_50:
return
f = Rectangle._meta.get_field("area")
result = FilterSet.filter_for_field(f, "area")
self.assertIsInstance(result, NumberFilter)
self.assertEqual(result.field_name, "area")

def test_filter_found_for_autofield(self):
f = User._meta.get_field("id")
result = FilterSet.filter_for_field(f, "id")
Expand Down Expand Up @@ -256,7 +271,6 @@ def test_unknown_field_ignore_behavior(self):
def test_unknown_field_invalid_initial_behavior(self):
# Creation of new custom FilterSet to set initial field behavior
with self.assertRaises(ValueError) as excinfo:

class InvalidBehaviorFilterSet(FilterSet):
class Meta:
model = NetworkSetting
Expand Down Expand Up @@ -421,7 +435,6 @@ class Meta:

def test_model_no_fields_or_exclude(self):
with self.assertRaises(AssertionError) as excinfo:

class F(FilterSet):
class Meta:
model = Book
Expand Down Expand Up @@ -548,7 +561,6 @@ def test_meta_fields_list_containing_unknown_fields(self):
msg = "'Meta.fields' must not contain non-model field names: " "other, another"

with self.assertRaisesMessage(TypeError, msg):

class F(FilterSet):
username = CharFilter()

Expand All @@ -560,7 +572,6 @@ def test_meta_fields_dict_containing_unknown_fields(self):
msg = "'Meta.fields' must not contain non-model field names: other"

with self.assertRaisesMessage(TypeError, msg):

class F(FilterSet):
class Meta:
model = Book
Expand All @@ -575,7 +586,6 @@ def test_meta_fields_dict_containing_declarative_alias(self):
msg = "'Meta.fields' must not contain non-model field names: other"

with self.assertRaisesMessage(TypeError, msg):

class F(FilterSet):
other = CharFilter()

Expand All @@ -593,7 +603,6 @@ def test_meta_fields_invalid_lookup(self):
msg = "Unsupported lookup 'flub' for field 'tests.User.username'."

with self.assertRaisesMessage(FieldLookupError, msg):

class F(FilterSet):
class Meta:
model = User
Expand Down Expand Up @@ -822,6 +831,7 @@ def test_filterset_factory_base_filter_meta_fields(self):
class FilterSetBase(FilterSet):
class Meta:
fields = ["name"]

f1 = CharFilter()
f2 = CharFilter()

Expand All @@ -832,6 +842,7 @@ def test_filterset_factory_base_filter_fields_and_meta_fields(self):
class FilterSetBase(FilterSet):
class Meta:
fields = ["name"]

f1 = CharFilter()
f2 = CharFilter()

Expand Down