Justin,

Thanks - that does exactly what I was looking for. Initially, I too
had to refresh the page once before I saw a list of options, but using
fields instead of base_fields fixed that.

This may be a stupid question, but is there a recommended/established
way of reading the django code. I am still fairly new to python, so I
don't quiet understand the nitty gritty's yet :(.

Best,

R

On Fri, Apr 11, 2008 at 8:50 AM, Justin <[EMAIL PROTECTED]> wrote:
>
>  In the __init__ method of the newforms.forms.BaseForm object, the
>  initialized fields
>  are copied as the fields as seen below
>
>  # The base_fields class attribute is the *class-wide* definition of
>  # fields. Because a particular *instance* of the class might want to
>  # alter self.fields, we create self.fields here by copying
>  base_fields.
>  # Instances should always modify self.fields; they should not modify
>  # self.base_fields.
>  self.fields = deepcopy(self.base_fields)
>
>
>  When I updated the attributes queryset like this:
>
>  form.base_fields['tasks'].queryset = tasks,
>
>  it was making the change to a stale copy of list.
>
>  Once I changed my code to say:
>
>  form.fields['tasks'].queryset = tasks,
>
>  everything worked out exactly how I planned.
>
>  Also, after the form has been submitted, you'll have to update the
>  queryset again like:
>  form = TestForm(request.POST)
>  form.fields['tasks'].queryset = Task.objects.filter(project=project)
>   if form.is_valid():
>  ....
>
>  This way, it will validate against the same queryset that the
>  selection was made from, otherwise, it'll throw a ValidationError
>
>
>
>
>  On Apr 10, 1:05 pm, Justin <[EMAIL PROTECTED]> wrote:
>  > You can also just call
>  >
>  > form.base_fields['tasks'].queryset =  tasks
>  >
>  > which will do this:
>  >
>  > form.base_fields['tasks']._set_queryset( tasks )
>  >
>  > How I found it?  I actually looked at the django code.  As you said in
>  > your post, I've never seen it documented anywhere.  I came across it
>  > after I found it in a few bug reports because apparently, there were
>  > similar issues.  I posted that ticket in my first post.
>  >
>  > When you use this, are you having the same issues with the page
>  > reloading without the set queryset until it's reloaded a second time?
>  >
>  > On Apr 10, 3:36 am, "Rishabh Manocha" <[EMAIL PROTECTED]> wrote:
>  >
>  > > I would like to get an answer for this too. Just reading through
>  > > Justin's code, I have figured out the solution to a problem I was
>  > > stuck on for a few days (I am new to Django so reading and learning
>  > > about the _set_queryset function was a godsend).
>  >
>  > > Also, I was wondering if someone can point me in the direction of any
>  > > documentation which describes other *obscure* function calls like
>  > > _set_queryset etc. I didn't see it anywhere in the main documentation
>  > > section. Had I seen it, I probably would have saved myself a few hours
>  > > of pulling my hair out.
>  >
>  > > Best,
>  >
>  > > ROn Wed, Apr 9, 2008 at 11:19 PM, Justin <[EMAIL PROTECTED]> wrote:
>  >
>  > > >  I noticed when looking at some code that after setting the queryset,
>  > > >  it appears in the form.  I can call ._get_queryset() or .queryset and
>  > > >  it looks updated.
>  >
>  > > >  The problem occurs in the actual form rendered.
>  >
>  > > >  form.as_ul() does not have the updated choices for the checkbox.  I'm
>  > > >  digging through django code but still nothing.
>  >
>  > > >  On Apr 8, 11:05 pm, Justin <[EMAIL PROTECTED]> wrote:
>  > > >  > I have a form:
>  >
>  > > >  > class TestForm(forms.Form):
>  > > >  >     name = forms.CharField(max_length=128, label="Test name")
>  > > >  >     tasks = forms.ModelMultipleChoiceField(required=False,
>  > > >  > label="Tasks", widget=forms.CheckboxSelectMultiple,
>  > > >  > queryset=Task.objects.none())
>  >
>  > > >  > The goal is to have checkboxes for a set of models.... easy enough. 
>  I
>  > > >  > initialize it to none because my goal is to update the queryset from
>  > > >  > within the views based on specific conditions.
>  >
>  > > >  > Inside my views.py, I have this:
>  >
>  > > >  > >>After clicking link to create project
>  >
>  > > >  > tasks = Task.objects.filter(project=project)
>  > > >  > form = TestForm()
>  > > >  > form.base_fields['tasks']._set_queryset( tasks )
>  > > >  > data = { "form": form, "project":project }
>  > > >  > return render_to_response("projects/create.html", data)
>  >
>  > > >  > When I click a link to this page from another, the checkboxes are 
> not
>  > > >  > there because the tasks queryset is still set to 
> Task.objects.none().
>  > > >  > If I hit F5 to reload, the correct tasks based on the project show
>  > > >  > up.  I can hit F5 infinitely and it's correct.  It's only on that
>  > > >  > first load.
>  >
>  > > >  > More weird...  I click back out to a different project page.  When I
>  > > >  > click the link, I expect it to show the form with a new set of tasks
>  > > >  > based on the second project as choices.  Instead of the correct
>  > > >  > choices or even none, it shows the choices from the first project.
>  > > >  > When I hit F5, it shows the correct choices.
>  >
>  > > >  > It's almost like it's caching the previous state between forwards.
>  >
>  > > >  > I also tried overriding __init__ in the form.  It followed the exact
>  > > >  > same behavior.  I'm using the most recent development version of the
>  > > >  > source.
>  >
>  > > >  > This ticket:http://code.djangoproject.com/ticket/4787looksrelated
>  > > >  > but it seems to have been resolved.
>  >
>  > > >  > Can I update the queryset like this?
>  >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to