On Jun 29, 2:24 pm, John Aherne <[EMAIL PROTECTED]> wrote: > OK. I did some more experimenting with ModelChoiceField and my table. > > Surprise. It worked when I thought it would not. Why did it work. By > luck in my model I had the __unicode__ function return the right value. > So now I could see better what was happening. > > I also printed out the post values in the 'runserver' so I could see > better there what was being selected. There I was initially confused > because the cleaned_data was the return value from __unicode__ and not > what I wanted. > > But if I looked at the POST data direct I had the correct ID I wanted. > > The I saw what was happening. The post was rcmcli01:3, so the table was > being called with the code and giving me the name. But what I really > want at this point is the code so I can use it to look up another table > for a list of jobs. For the moment I can take that directly from the > POST data but I should really be getting it from cleaned_data. Not sure > how I can do that at the moment. > > Thanks > > John Aherne
I think you're being confused by the display of what's being returned. In fact the value in cleaned_data should be the actual *model instance*. Since you have __unicode__ defined on that model, when you do >>> value = myform.cleaned_data['mychoicefield'] >>> print value you will see the value of the unicode method you defined. But the underlying value is the actual element - you can do >>> print value.id to get the id, and so on. By the way, I haven't addressed the second part of your OP, which was how to dynamically filter the values in the choicefield according to session data. Here's a good post that explains that: http://oebfare.com/blog/2008/feb/23/changing-modelchoicefield-queryset/ -- 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---