specify querysets in forms

7 views
Skip to first unread message

David Crandell

unread,
Jul 20, 2021, 11:03:07 AM7/20/21
to Django users
Hello again. I know I'm posting in here all the time.

I am trying to get specific querysets in my forms based on the id in the kwargs but it says there is a keyerror with 'id'. It's the same kwargs.get('id') I reference whenever I do other stuff. Not sure why it doesn't like it this time. Any suggestions?

class LocGroupForm(forms.ModelForm):
class Meta:
model = LocationsGroup
fields = ('group_name', 'slug', 'customer', 'location', 'user_id',)

def __init__(self, *args, **kwargs):
qs = kwargs.pop('id')
super(LocGroupForm, self).__init__(*args, **kwargs)
self.fields['customer'].queryset = Customers.objects.get(pk=qs)
self.fields['location'].queryset = CustomerLocations.objects.filter(customer_id=qs)
self.fields['user_id'].queryset = CustomerUsers.objects.filter(customer_id=qs)
Reply all
Reply to author
Forward
0 new messages