Skip to content Skip to sidebar Skip to footer

Creating A Drop-down Menu Of Member Ids

I have a model Member that includes a specification called member_id. class Member(models.Model): member_id = models.SlugField(max_length=10) name = models.CharField(max_le

Solution 1:

Member.objects.values('member_id') gives you list of dictionaries with values structured as {'member_id': value}.

If you just want the ids, you can make Member.objects.values_list('member_id', flat=True) which will just give you list of member ids.

Anyways you try to use ModelChoiceField which expects a queryset of Model instances and you try to pass to it a list of slugs.

What you want to do is ModelChoiceField(queryset=Member.objects.all(), to_field_name='member_id') Normally ModelChoiceFields gets id as option, but you can override it with to_field_name :

https://github.com/django/django/blob/master/django/forms/models.py#L1129-L1144

Post a Comment for "Creating A Drop-down Menu Of Member Ids"