[Answer]-ChoiceField from model – Django – MongoEngine

1👍

So, what you need is to instantiate the Form_save_brand with a specific peer so you can work out the dependencies and what is shown. This can be achieved by this (most of the code is kept the same)

You have to change the form to the following:

class Form_save_brand(forms.Form):
    name = forms.CharField()

    campaigns = forms.ChoiceField()
    peers_partner = forms.ChoiceField()
    payments = forms.ChoiceField()
    medias = forms.ChoiceField()
    socials = forms.ChoiceField()

    def __init__(self, peer, *args, **kwargs):
        # we pass a peer parameter that will be used in the queryset query
        self.instance = kwargs.pop('instance', None)
        self.fields['admins'] = forms.ModelChoiceField(queryset=Brand.objects.get_or_create(id=peer),empty_label="")

and then, on your view

class AddBrand(CreateView):
    model = Brand
    # you don't need a form class here
    def get_form(self, form_class=None):
        return Form_save_brand(<you set the peer value here>, **self.get-form_kwargs())

Leave a comment