John Aherne wrote: > Daniel Roseman wrote: > >> On Jun 29, 11:34 am, John Aherne <[EMAIL PROTECTED]> wrote: >> >> >>> I've been looking at django for a while. Been through the tutorial a few >>> times and read 2 books several times and am still trying to find out how >>> some things work. >>> >>> Instead of an empty form on first loading, I would like to be able to >>> fill out some selection lists with data pulled from a database table. >>> >>> There are 2 things I would like to achieve:-- >>> >>> First, I have a model with 30 fields but I want to extract 2 columns for >>> use with a selection list. I want to get all rows in the table but only >>> 2 columns. I have looked at using .values() but that returns a different >>> format than a queryset. >>> >>> So I then found Managers. But before I start going through that I >>> thought I would check and see if I have gone blind and missed some >>> obvious way of getting my select list filled based on my table selection. >>> >>> Second thing is I want to select certain columns from a table but filter >>> the selection based on input from the session data. At the moment, I'm >>> not using session data, I just want to hardcode the options so I can >>> concentrate on how I get my selection list filled. Once again my model >>> has 10 fields, but I only want to get 2 of those to use in my selection >>> list. >>> >>> I thought about filling out a dictionary and passing that in as per the >>> samples in forms.py. But I then read that I should not be doing this. >>> Choices in forms is for mainly static selections. So I then start >>> looking for a way to get my data set up for a selection list. But as I >>> say, I've looked at .objects.values() and then started to look at managers. >>> >>> Also I have googled all over for some reference to this problem I have >>> and not found any link to my problem. I have some found some links that >>> relate to some edge cases, but nothing simple. So I think I must be >>> being a bit stupid or blind, since noone else is having this problem. >>> >>> I would be very pleased if someone could point me in the way to go on this. >>> >>> John Aherne >>> >>> >> What you want is a ModelChoiceField. For example: >> >> from django import newforms as forms >> class MyForm(forms.Form): >> mychoicefield = >> forms.ModelChoiceField(queryset=MyModel.objects.all()) >> >> or you could put your filter as the queryset - >> MyModel.objects.filter(field=criteria) >> >> -- >> DR. >> >> >> > Thanks for the reply. > > Yes I did look at this. But this pulls in all the fields from the model. > I only want to pull in 2 columns from the table. > > So I looked at .objects.values() and then managers. But before I take > that route I wanted to check I was going in the right direction. > > You seem to imply that I need to set up a model with just the fields I > need. Where I have found samples to look at that is the option chosen, > which makes it a lot simpler. > > Am I missing something. > > John Aherne > > > > > > 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---