24👍
✅
The only answer to ‘is it correct’ is ‘does it work when you run it?’ The answer to that of course is no, so I don’t know why you’re asking here.
There’s no way to use limit_choices_to dynamically to limit based on the value of another field in the current model. The best way to do this is by customising the form. Define a ModelForm subclass, and override the __init__
method:
class MyOrderForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyOrderForm, self).__init__(*args, **kwargs)
if 'initial' in kwargs:
self.fields['customer'].queryset = Customer.objects.filter(account=initial.account)
0👍
You should set choices
field of your order form (inherited from ModelForm
) in the constructor.
- [Django]-Update to Django 1.8 – AttributeError: django.test.TestCase has no attribute 'cls_atomics'
- [Django]-How to load fixtures only once in django unit tests ?
- [Django]-Django TypeError: 'RelatedManager' object is not iterable
-2👍
limit_choices_to={'account': 'self.account'}
is wrong, since foreign key to customer cannot point to Account
.
- [Django]-Django: Filter a Queryset made of unions not working
- [Django]-Django – New fonts?
- [Django]-'dict' object has no attribute 'id'
Source:stackexchange.com