Help with data bound ModelChoiceField

48 views
Skip to first unread message

progm...@gmail.com

unread,
Dec 11, 2018, 4:34:47 PM12/11/18
to Django users
Hi,
I'm struggling to set up a data-bound(queryset) ModelChoiceField. Here is the forms.py code to populate the control:

class frmCustomerList(forms.Form):
customerlist = forms.ModelChoiceField(queryset=dbCustomer.objects.values('name').order_by('name').distinct())

This is working but is formatted incorrectly. It displays as:

choices.png



I actually need only the 'Customer 1' to show. In fact, I would like the database's primary key('id') as a hidden key or value in the control and use the actual data('Customer 1') only for the visible display.

I can't work it out. if I add 'id' to the queryset fields I get this:


choices2.png



Can I get some examples, maybe some explanations also.


Thanks


madjardi

unread,
Dec 11, 2018, 4:50:46 PM12/11/18
to Django users
def __str__(self):
       return "Custome" + self.id  

madjardi

unread,
Dec 11, 2018, 4:51:49 PM12/11/18
to Django users
Inside dbCustomer

среда, 12 декабря 2018 г., 0:50:46 UTC+3 пользователь madjardi написал:

progm...@gmail.com

unread,
Dec 11, 2018, 4:58:43 PM12/11/18
to Django users
Thank you for your suggestion:
Here is my model, as you will see, I commented out my def and added yours. Unfortunately there was no change.


# Customer Information Database
class dbCustomer(models.Model):
name =models.CharField(max_length=60)
contactfn =models.CharField(max_length=25)
contactln =models.CharField(max_length=25)
email =models.EmailField(blank=False,max_length=254)
phone =models.CharField(max_length=25)
shpaddr1 =models.CharField(max_length=60)
shpaddr2 =models.CharField(max_length=60)
shpaddr3 =models.CharField(max_length=60)
shpcity =models.CharField(max_length=30)
shpstate =models.CharField(max_length=2)
shpzip =models.CharField(max_length=11)
biladdr1 =models.CharField(max_length=60)
biladdr2 =models.CharField(max_length=60)
biladdr3 =models.CharField(max_length=60)
bilcity =models.CharField(max_length=30)
bilstate =models.CharField(max_length=2)
bilzip =models.CharField(max_length=11)

# def __str__(self):
# return '%s' % (self.name)
def __str__(self):
return "Customer" + self.id

madjardi

unread,
Dec 11, 2018, 5:05:35 PM12/11/18
to Django users
I use django 2.1.4 python 3.6

среда, 12 декабря 2018 г., 0:58:43 UTC+3 пользователь progm...@gmail.com написал:
Screenshot from 2018-12-12 01-04-49.png
Screenshot from 2018-12-12 01-04-46.png

madjardi

unread,
Dec 11, 2018, 5:16:07 PM12/11/18
to Django users
aybe it help You 
https://stackoverflow.com/questions/42157384/django-how-to-change-value-of-forms-modelchoicefield

среда, 12 декабря 2018 г., 0:34:47 UTC+3 пользователь progm...@gmail.com написал:

maunish dave

unread,
Dec 12, 2018, 12:15:10 AM12/12/18
to django...@googlegroups.com
I think you should just call all object instead of using values ( ) and __str__ will give you the names of customer
I am not sure but this should work

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0a1b00b9-43d7-4785-aab4-34d141b0c22a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

progm...@gmail.com

unread,
Dec 12, 2018, 12:17:09 AM12/12/18
to Django users

TY madjardi for all your input! I continued to search and found what I wanted. But I still need to understand how it all works. I know it is an initiation function on the form but not sure how variables are transported around to give me the results. But I think I can dig further to find out.
Here is what works for me:
class frmCustomerList(forms.Form):
name = forms.ChoiceField(choices = [])

def __init__(self, *args, **kwargs):
super(frmCustomerList, self).__init__(*args, **kwargs)
self.fields['name'].choices = [(x.pk, x.name) for x in dbCustomer.objects.all()]

Glen D souza

unread,
Dec 13, 2018, 4:04:50 AM12/13/18
to django...@googlegroups.com
In the First Screenshot the code is 
def __str__(self):
return "Customer" + "self.id"



Remove the quotes from "self.id"

def __str__(self):
return "Customer" + self.id


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages