On May 6, 8:57 am, Daniel Roseman <[email protected]>
wrote:
> On May 6, 5:26 am, NicoEchániz <[email protected]> wrote:
> > Hello,
>
> > I have a model which has a ForeignKey to itself. I'm doing this to
> > represent a hierarchy which I then need to display in the
> > corresponding select field of my form.
>[....]
> > You can find the corresponding code snippet
> > here:http://dpaste.com/hold/41342/
>
> Your code in lines 22 onwards is being executed when the form class is
> defined. So the choices for robro_superior are being set at that
> point, and not updated.
>
> To fix this, put this code into an __init__() method. This is called
> when the form is *instantiated* - so each time, it will get the list
> fresh from the database.
>
> class RubroForm(ModelForm):
> rubro_superior = ModelChoiceField(queryset=Rubro.objects.all(),
> required=False)
>
> def __init__(self, *args, **kwargs)
> super(RubroForm, self).__init__(*args, **kwargs)
> r = Rubro.objects.all()
> choices = [('', '- - - -')] + [(rubro.id, rubro.jerarquia) for
> rubro in r]
> choices.sort(key=itemgetter(1))
> rubro_superior.choices = choices
>
> --
> DR.
Daniel,
Thanks for your reply.
I had already tried this, but there's another problem whith this
solution. The class attribute rubro_superior seems inaccesible for
some reason.
I get the error:
global name 'rubro_superior' is not defined
I've tried changing the offending line:
rubro_superior.choices = choices
for:
RubroForm.rubro_superior.choices = choices
and for:
self.__class__.rubro_superior.choices = choices
to no avail...
so... the description of the problem seems correct to me, but I can't
find the way to solve it.
Thanks,
NicoEchániz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---