How to select from the second field in the form - filtering data depending on the values of the first field?
Is it possible to somehow adapt something similar to my problem?
I would like a list to appear in the form based on data from another list Select from the second field - filtering data - (contents) depending on the received values of the first field. For example. London Cafe London Restaurant London Fast Food Manchester Pizzeria Manchester Burgers
I select a city in the form field and the second field is filtered by establishments.
How to select from the second field in the form - filtering data depending on the values of the first field?
class ExcludedDateForm(ModelForm):
class Meta:
model = models.ExcludedDate
exclude = ('user', 'recurring',)
def __init__(self, user=None, **kwargs):
super(ExcludedDateForm, self).__init__(**kwargs)
if user:
self.fields['category'].queryset = models.Category.objects.filter(user=user)
class FilterByUserMixin(LoginRequiredMixin):
"""
Filters the queryset with `self.request.user` in a specified `user_field` field. `user_field` defaults to "user".
"""
user_field = "user"
def get_queryset(self, *args, **kwargs):
return (
super()
.get_queryset(*args, **kwargs)
.filter(**{self.user_field: self.request.user})
)
class Book(models.Model):
author = models.ForeignKey(MyUser, on_delete=models.RESTRICT)
number_pages = models.PositiveIntegerField()
title = models.CharField(max_length=1000)
class BookUpdateView(FilterByUserMixin, UpdateView):
model = Book
template_name = "book/my_book_update_template.html"
fields = ["number_pages", "title"]
success_url = reverse_lazy("book-update-success")
user_field = "author"
. . .. .. ........