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

'NoneType' object has no attribute 'Meta' with django-filter and through m2m model #320

Closed
lerela opened this issue Mar 1, 2021 · 2 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@lerela
Copy link

lerela commented Mar 1, 2021

Describe the bug
Using django-filter backend with a through relationship fails with 0.13.2. Not sure that's the exact issue but this does not happen without the "through".

To Reproduce
Models:

class Reltd(models.Model):
    test = models.CharField(max_length=50)

class TestModel(models.Model):
    reltd = models.ManyToManyField(Reltd, through="TestModelReltd")

class TestModelReltd(models.Model):
    tm = models.ForeignKey(TestModel, on_delete=models.PROTECT)
    rl = models.ForeignKey(Reltd, on_delete=models.PROTECT)

Views:

class MyFilter(filters.FilterSet):
    reltd = filters.ModelMultipleChoiceFilter(
        field_name="reltd", label="reltd"
    )

    class Meta:
        model = TestModel
        fields = ["reltd"]

class TestViewSet(viewsets.ModelViewSet):
    queryset = TestModel.objects.all()
    serializer_class = TestSerializer

    filter_backends = [
        filters.DjangoFilterBackend,
    ]
    filter_class = MyFilter 

Error:

  File "test_spectacular/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "test_spectacular/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "test_spectacular/venv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "test_spectacular/venv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/management/commands/spectacular.py", line 50, in handle
    schema = generator.get_schema(request=None, public=True)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/generators.py", line 219, in get_schema
    paths=self.parse(request, public),
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/generators.py", line 196, in parse
    operation = view.schema.get_operation(path, path_regex, method, self.registry)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 61, in get_operation
    parameters = self._get_parameters()
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 190, in _get_parameters
    **dict_helper(self._get_filter_parameters()),
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 365, in _get_filter_parameters
    parameters += filter_extension.get_schema_operation_parameters(self)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/contrib/django_filters.py", line 43, in get_schema_operation_parameters
    result += self.resolve_filter_field(
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/contrib/django_filters.py", line 95, in resolve_filter_field
    schema = auto_schema._map_model_field(model_field, direction=None)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 407, in _map_model_field
    return self._map_serializer_field(field, direction)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 480, in _map_serializer_field
    schema = self._map_serializer_field(field.child_relation, direction)
  File "test_spectacular/venv/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 494, in _map_serializer_field
    model = field.parent.parent.Meta.model
AttributeError: 'NoneType' object has no attribute 'Meta'

This happens in 0.12, 0.13.0, 0.13.1 & 0.13.2 as field.parent.parent seems to be None in this case.

Expected behavior
No issue.

@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Mar 2, 2021
@tfranzel
Copy link
Owner

tfranzel commented Mar 2, 2021

@lerela indeed this was a very special case that surfaced nowhere else. i far exceeded the time i have for django-filter because the library keeps on generating more edge cases. i opted to catch errors from now on to not break schema generation because of django-filter.

@lerela
Copy link
Author

lerela commented Mar 2, 2021

That makes total sense. Sane fallbacks are totally fine! Thanks again for all your great work (this solves that issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants