choice model with objects.values_list options

86 views
Skip to first unread message

GoSantoni

unread,
Dec 19, 2009, 10:27:05 PM12/19/09
to Django users
Hey just a newbie question about the design of the models.py What is
the best way to use the result of objects.values_list for a choices
model ?

********************** shell *******************
In [1]: from photos.models import Image, Pool

In [3]: Image.objects.values_list('image')
Out[3]: [(u'photologue/photos/wielrenfiets.jpg',), (u'photologue/
photos/example2.jpg',)]


********************** models.py ***************

"""Post model."""
URL_CHOICES = (
(1, _('................ generated from image list (so
wielrenfiets.jpg) ......... ')),
(2, _('................ generated from image list (so
example2.jpg )......... ')),
(3, _('................ generated from image list .........
')),
etc
)

url = models.IntegerField(_('url'), choices=URL_CHOICES,
default=1)
************************

Is this something for a ModelChoiceField or Formset ? This replacement
failed
url = ModelChoiceField(queryset=Image.objects.values_list
('image'))

thanks in advance

GoSantoni

unread,
Dec 19, 2009, 11:26:41 PM12/19/09
to Django users

Found this (old) post http://oebfare.com/blog/2008/feb/23/changing-modelchoicefield-queryset/

So i tried

class Post(models.Model):
blog = models.ForeignKey(blog)
.....
url = models.ModelChoiceField
(queryset=Image.objects.values_list())

def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)
self.fields["url"].queryset = Image.objects.values_list
('image')

But i run into an AttributeError: 'module' object has no attribute
'ModelChoiceField'

Daniel Roseman

unread,
Dec 20, 2009, 5:53:35 AM12/20/09
to Django users
On Dec 20, 4:26 am, GoSantoni <mj.schuur...@gmail.com> wrote:
> Found this (old) posthttp://oebfare.com/blog/2008/feb/23/changing-modelchoicefield-queryset/

You've misread Brian's post. The url redefinition, and the overridden
__init__, belong in the **form**, not the model. When you define your
ModelForm, use those definitions there.
--
DR.

Mark Schuuring

unread,
Dec 20, 2009, 2:57:55 PM12/20/09
to django...@googlegroups.com
Dear Daniel,
Thanks for your quick response. As you suggested i applied the code to
the forms.py http://dpaste.com/135885/ Though it fails on line 25
self.fields["url"].queryset = Image.objects.values_list('image') with
a KeyError. Does this suggest a failure in line 20 because to my
knowledge the KeyError appears when a field doesn't exist. Or do i
have to alter line 25? The Image library is loaded in line 8

Regards


2009/12/20 Daniel Roseman <dan...@roseman.org.uk>:

> --
>
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>
>

--
Mark Schuuring
M: mj.sch...@gmail.com

Reply all
Reply to author
Forward
0 new messages