G
G
Gleb2021-01-15 18:02:47
Django
Gleb, 2021-01-15 18:02:47

How to sort option in select in django?

How can sort before output in select option in django?
I have two tables with categories (which is created by the user himself) and articles. And when adding to the form, it is necessary that the categories in the form of options are displayed in the select, only those that the user himself created.
I tried to draw output through for, but the problem is that I don't know how to save it to the database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2021-01-15
@paundaa

How to sort option in select on django

it is necessary that the categories in the form of options would be displayed in the select, only those that the user himself created.

Sort and filter are two different operations, don't confuse them. What to filter by user, they usually override the __init__ method of the form, adding user as an argument and, well, an example code like this
self.fields['category'].queryset = Category.objects.filter(user=user)

PS in fact, this is a hundred times sucked up and easily googled.

A
alternativshik, 2021-01-15
@alternativshik

in the modelform, in the select field, set the queriset with the desired filter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question