On Dec 31, 11:13 am, phoebebright <phoebebri...@spamcop.net> wrote:
> I have a model that has a car and a car has a foreign key to carmodel
> via the field model  (too many models!)
>
> This is the line for the form which appears to run:
>
> carmodel=forms.ModelChoiceField(queryset=Car.objects.all().values
> ('model','model__name').distinct() )
>
> It generates the correct SQL
>
> SELECT DISTINCT `carpoint_car`.`model_id`, `carpoint_carmodel`.`name`
> FROM `carpoint_car` INNER JOIN `carpoint_carmodel` ON
> (`carpoint_car`.`model_id` = `carpoint_carmodel`.`id`)
>
> and if I run this manually I get 6 records returned:
>
> 6       Galaxy
> 7       Bigun
> 8       Touring
> 9       Espace
> 10      D1
> 11      Tank
>
> But I don't get the form being displayed.  If I remove this line the
> form displays okay.
>
> What pattern is ModelChoiceField expecting in the queryset to know
> what fields to use to generate a choice from?

You don't need the values() part. Django will use the id and the
__unicode__ method or a normal queryset to generate the choices. If
you want something different, use a standard integer field with a
Select widget, and populate the choices manually.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to