D
D
Daria2020-03-06 17:26:10
Django
Daria, 2020-03-06 17:26:10

How to hide some option in Django select?

There is a list of choices in models.py that is displayed via the form.py page, but some options need not be displayed. How to do? Options with JS are not suitable.

class EventForm(ModelForm):
    class Meta:      
        def sort():
            status = []
            for i in Event.PROBLEM_CHOICES_STATUS:
                if i[0] == 1 or i[0] == 2:
                    status.append(i)
            return status
        
        Event.PROBLEM_CHOICES_TYPE = sort()

        model = Event
        fields = ( 'status', )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseny, 2020-03-06
@Sestokova

You can check the transmitted options at the view level and then they will not get into the page in principle.
In principle, it doesn’t matter where you will do the check, the main thing is that you have something like:

choices = [1, 2]
if not false:
    choices.append(3)

In the list, of course, there can be an object of a key-value pair, I'm talking about logic here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question