1 2 3 4 5 6 7 8 9 10 | class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['myfield'].queryset = \
MyOtherModel.objects.filter(
Q(active=True)|Q(pk=self.instance.myfield_id))
class Meta:
model = MyModel
|
More like this
- CustomChoiceField, Selectable label field version of ModelChoiceField by mauro 5 years, 2 months ago
- Changing queryset of a ModelChoiceField by mimog 1 year, 10 months ago
- Lazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view by jpic 4 years, 4 months ago
- Reorder fields directly in the ModelForm by HM 5 years ago
- Q marshaller by Spike^ekipS 5 years, 2 months ago
Comments
Thanks for this Rob, I was just trying to figure this out today...
#
Much cleaner than the hack I was using for this. Thanks!
#