N
N
NyxDeveloper2021-02-01 10:16:53
Django
NyxDeveloper, 2021-02-01 10:16:53

How to pass initial parameter to select multiple field of django form?

I've been struggling with this issue for a long time, but I couldn't google it. I'm adding a user to groups when creating, I decided to use a multiselect. Everything works as I wanted, but when editing the user, I wanted to display the values ​​of the groups already assigned to him in the select field, but when passing the QuerySet values ​​from the user's groups to form.base_fields['groups'].initial, they are added to the initialization parameters, but not displayed on the page.
The form and view looks like this:

class EditSotrForm(forms.ModelForm):
    class Meta:
        model = Sotr
        fields = [
            'firstName',
            'lastName',
            'middleName',
            'idDolj',
            'description'
        ]

        widgets = {
            'idDolj': forms.Select(attrs={
                'class': 'form-control'
            }),
        }

    groups = forms.ModelChoiceField(queryset=Group.objects.all(), widget=forms.SelectMultiple(attrs={
        'class': 'select2bs4 select2-hidden-accessible form-control',
        'multiple': '',
        'tabindex': '-1',
        'aria-hidden': 'true'
    }))

    login = forms.CharField()
    password = forms.CharField()
    confirmPassword = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(EditSotrForm, self).__init__(*args, **kwargs)
        self.fields['firstName'].widget = forms.TextInput(attrs={'class': 'form-control'})
        self.fields['lastName'].widget = forms.TextInput(attrs={'class': 'form-control'})
        self.fields['middleName'].widget = forms.TextInput(attrs={'class': 'form-control'})
        self.fields['login'].widget = forms.TextInput(attrs={'class': 'form-control'})
        self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'form-control'})
        self.fields['confirmPassword'].widget = forms.PasswordInput(attrs={'class': 'form-control'})
        # self.fields['idDolj'].widget = forms.Select(attrs={'class': 'form-control'})
        self.fields['description'].widget = forms.Textarea(attrs={'class': 'form-control'})

        self.fields['idDolj'].required = False
        self.fields['groups'].required = False
        self.fields['description'].required = False
        self.fields['firstName'].required = False
        self.fields['lastName'].required = False
        self.fields['middleName'].required = False
        self.fields['login'].required = False
        self.fields['password'].required = False
        self.fields['confirmPassword'].required = False


@login_required
def EditSotrView(request, objId):
    if request.method == 'GET':
        form = EditSotrForm(instance=Sotr.objects.get(id=objId))
        form.base_fields['groups'].initial = Sotr.objects.get(id=objId).user.groups.constrained_target
        return render(request, 'edit_sotr.html', {
            'form': form,
            'objId': objId
        })


Maybe I'm doing something wrong, tell me, who has already encountered such a problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NyxDeveloper, 2021-02-26
@NyxDeveloper

initial fields are of type list by default, so you can pass it a list consisting of the desired id objects of the model you are interested in (the main thing is that this model matches the type of objects in the queryset field)

form.base_fields['field_name'].initial = [
    i for i in form.base_fields['field_name'].choices.queryset.filter(id__in=[1, 2, 3, 4])
]

S
siarheisiarhei, 2021-02-04
@siarheisiarhei

this is fundamentally not possible either by means of dj or js ....
I got out by inserting the form (html) and inserting initial={{ tag }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question