python - Combine two ModelForm fields in Django -


I have a boolean field and a foreign key model

  class ModelA (model) Model): is_anonymous = BooleanField (default = Incorrect) page = External key (ModelB, null = true)  

I want to add these two fields to a selected field.

There is an option between and and ModelB should have an option for each object.

So if any page & lt; Select & gt; If selected in the area is_anonymous , then it should be incorrect and if anonymous selection is selected, then the field should be typed page and is_anonymous Must be correct.

I think I must add ModelB.objects.all () to the options list in the query

  Self. Field ['field_name']. Widget = form. Select (option = [(false, "not unknown"), (true, "anonymous"),])  

but this will save the field f Ield_name which is wrong.

There is no need to combine the area here. You can only use 'anonymous' for the foreign key field Set up, and handover it in a clean way.

ModelAForm (forms.ModelForm): page = forms.ModelChoiceField (queryset = ModelB.objects.all (), empty_value = "Anonymous") class meta: model = model B.Def clean ( Self): If not self.cleaned_data ['page']: self.cleaned_data ['anonymous'] = true

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -