carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections
https://django-filter.readthedocs.io/en/main/
Other
4.45k stars 766 forks source link

Verbose field name is [invalid name] when field spans ManyToOneRel relationship #1571

Open bmihelac opened 1 year ago

bmihelac commented 1 year ago

When field spans ManyToOneRel relationship, verbose_field_name returns '[invalid name]'.

Here is how to reproduce it with models already included in most Django projects.

>>> from django_filters.utils import verbose_field_name
... from django.contrib.contenttypes.models import ContentType
... 
... ContentType.objects.filter(permission__id=1)
... verbose_field_name(ContentType, "permission__id")
'[invalid name]'

Reason is that part does not have related_name, when it is ManyToOneRel .

This is in https://github.com/carltongibson/django-filter/blob/main/django_filters/utils.py#L264

Function could check if part is instance and appent verbose name of related model, ie:

      if isinstance(part, models.ManyToOneRel):
          names.append(force_str(part.related_model._meta.verbose_name))
      elif isinstance(part, ForeignObjectRel):

I can create PR for this.

priyank-panchal commented 1 year ago

As a beginner in open source, I understand the problem and would like to generate a pull request (PR) to address it.

carltongibson commented 1 year ago

Happy to look at a PR. Please include regression tests.