J
J
JRazor2018-10-04 01:58:19
Django
JRazor, 2018-10-04 01:58:19

Django-Filters: in select, email is passed instead of the user field. How to fix?

Good night or morning. Someone has something. We have the following models:

class City(models.Model):
  name = models.CharField()

class User(models.Model):
  city = models.ForeignKey(City)

We need to return a list of cities specified by users. I'm trying to pass data to the filter:
city = filters.ModelChoiceFilter(
        queryset=User.objects.all(),
        field_name='city',
        to_field_name='city',
        label=_('Filter by city:'),
    )

    class Meta:
        model = User
        fields = {
            'city__name': ['exact',]
        }

It displays the values ​​normally (city id), but the text itself is not a city, but an email. Can anyone advise how to fix this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-10-04
@pyHammer

You must pass City.objects.all() to the queryset parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question