Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. class MyForm(forms.ModelForm):
  2.     class Meta:
  3.         model = MyModel
  4.  
  5.     def __init__(self, *args, **kwargs):
  6.         inst = kwargs.get('instance', None)
  7.         super(MyForm, self).__init__(*args, **kwargs)
  8.         self.fields['myfield'].queryset = inst.related_name.all()
  9.     # or
  10.     self.fields['myfield'].queryset = SomeModel.objects.filter(field=inst)
  11.     # or ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement