python - Combine two ModelForm fields in Django -
i have model boolean field , foreign key
class modela(models.model): is_anonymous = booleanfield(default=false) page = foreignkey(modelb, null=true) i want combine these 2 fields in select field.
it should have alternative <option value="anonymous">anonymous</option> , alternative each object in modelb.
so if page selected in <select> field is_anonymous should false , if anonymous selected field page should null , is_anonymous should true.
i think have add together queryset modelb.objects.all() selection list in
self.fields['field_name'].widget = forms.select(choices=[(false, "not anonymous"), (true, "anonymous"),]) but save result field field_name wrong.
there's no need combine fields here. can set empty_value foreign key field 'anonymous', , deal in clean method.
class modelaform(forms.modelform): page = forms.modelchoicefield(queryset=modelb.objects.all(), empty_value="anonymous") class meta: model = modelb def clean(self): if not self.cleaned_data['page']: self.cleaned_data['anonymous'] = true python django django-models django-forms django-views
No comments:
Post a Comment