I haven't done this one in a while, but you need to make the CHOICES a tuple or a list[1], not a queryset (unless there has been changes in this specific area).
something like this:
def make_choices():
choices = []
for item in Province.objects.all()
choices.append((item.name, item.name))
return choices
# your form stuff
...
widgets = { 'name': Select(choices=make_choices()), }
The first value is the value stored, the second one is the human readable form in the tuple.
Mike
[1] http://docs.djangoproject.com/en/1.2/ref/models/fields/#field-choices
--
Banacek's Eighteenth Polish Proverb:
The hippo has no sting, but the wise man would rather be sat upon
by the bee.
--
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.
Whats the forms __init__ method look like?
Mike
--
Q: What's hard going in and soft and sticky coming out?
A: Chewing gum.
for self.instance, you'll wnat to do something like this:
self.instance = kwargs.pop('instance')
or add:
def __init__(self, instance, *args, **kwargs):
self.instance = instance
as for the blank items, verify your query works as expected. After that I'm not sure.
Mike
--
If I felt any more SOPHISTICATED I would DIE of EMBARRASSMENT!
Er, no, he should be calling the super-class (ModelForm) constructor,
which would correctly handle instance being in kwargs and set
self.instance - which the OP is doing. Cargo cult programming is bad.
Cheers
Tom
On Thursday, February 24, 2011 05:15:09 am Tom Evans wrote:
> Cargo cult programming is bad.
>
> Cheers
>
> Tom
Not going to really argue the point, it's valid, but it could be based on habits taught by others, mentors...
Mike
--
Never, ever lie to someone you love unless you're absolutely sure they'll
never find out the truth.
Your form is a ModelForm for a UserProvince model, and you are passing
in an django.contrib.auth.User instance as the instance. This is
wrong!
If you are editing an existing UserProvince instance, then pass that
object to the form as the instance. If you are creating a new
UserProvince instance, then do not pass in instance.
If you want your form to automatically associate the user with the
instance, then that user should be passed to the form's constructor,
and override save() to associate it with the instance, eg:
class FooForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(FooForm, self).__init__(*args, **kwargs)
def save(commit=False):
foo = super(FooForm, self).save(commit=False)
if not foo.user:
foo.user = self.user
if commit:
foo.save()
return foo
Cheers
Tom
No exception supplied
Exception Type:
DoesNotExist
\django\db\models\fields\related.py in __get__, line 301
commit True
self <apps.accounts.forms.ProvinceForm object at 0x030F1F30>
foo Error in formatting: