Skip to content
New issue

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

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

Already on GitHub? # to your account

Select a valid choice error in ModelChoiceFilter #1552

Open
nikartom opened this issue Jan 22, 2023 · 3 comments
Open

Select a valid choice error in ModelChoiceFilter #1552

nikartom opened this issue Jan 22, 2023 · 3 comments

Comments

@nikartom
Copy link

The form is successfully generated and a list of streets is generated. But when selecting and trying to filter, the form displays an error Select a valid choice. That choice is not one of the available choices.
Model

class Address(models.Model):
    city = models.ForeignKey(City, on_delete=models.PROTECT, verbose_name="Город")
    street = models.CharField(max_length=255, verbose_name="Улица")
    numb = models.CharField(max_length=64, verbose_name="Номер дома")
   
    def __str__(self):
          return self.street

Filter

class ProjectFilter(django_filters.FilterSet):
    address = django_filters.ModelChoiceFilter(
        queryset=Address.objects.values_list('street', flat=True),
        widget= forms.Select(),
        label="Addrees",
        label_suffix="",
    )

    class Meta:
        model = Project
        exclude = [field.name for field in Project._meta.fields]
        order_by_field = 'address'

URL result
http://127.0.0.1:8000/table/?address=Forest

@carltongibson
Copy link
Owner

Address.objects.values_list('street', flat=True)

This is a list of streets, not model instances, so you're not making a "model choice".

@nikartom
Copy link
Author

Address.objects.values_list('street', flat=True)

This is a list of streets, not model instances, so you're not making a "model choice".

I understand that this is a list. But I select one value from the list and that value is correctly integrated into the url

@carltongibson
Copy link
Owner

But that's not a valid query set for a model choice field (which needs models, not strings)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants