Jorge, muchas gracias!

I have not got as close as a mile from manipulators yet, even though
they look pretty cool. I'll certainly save your code around for later.

Who would have thought my code was not that off-track as I thought! =)

Anyway, a big thank you for the time you took to write this, I really
appreciate it!!!

On 7/13/06, Jorge Gajon <[EMAIL PROTECTED]> wrote:
>
> Hi again Carlos,
>
> I suggested this change in your code:
> >     results = Vozilo.objects.all()
> >     if selected_model_id != 0:
> >         results = results.filter(model_id__exact=selected_model_id)
> >     if selected_gorivo_id != 0:
> >         results.extend(results.filter(gorivo_id__exact=selected_gorivo_id))
> >
> But I'm totally wrong there. Your original code should be fine,
> because you were chaining the filters. The 'extends' is not necessary
> and in fact it is wrong.
>
> I also realized that the filter logic in the code that I proposed is
> wrong. With that code you would get a list of cars that match the
> specified model, along with all the cars that match the gorivo type
> (regardless of model).
>
> The correct thing is to chain the filters like you did in your
> original code. This should work, I tested it with a model I have here.
>
>     results = Vozilo.objects.all()
>     if model:
>         results = results.filter(model=model)
>     if gorivo:
>         results = results.filter(gorivo=gorivo)
>
>
> I'm sorry if I caused any confusion and for not paying more attention
> to that filtering part :)
>
> Cheers,
> Jorge
>
>
>
> On 7/13/06, Jorge Gajon <[EMAIL PROTECTED]> wrote:
> > Hi Carlos,
> >
> > I think your code is going good so far. Of course I'm not a guru but I
> > think your code is fine. Is there something that you don't feel
> > comfortable with? With respect to adding the results of both filters,
> > you need to extend the results of the first results (if any), like
> > this:
> >
> >     results = Vozilo.objects.all()
> >     if selected_model_id != 0:
> >         results = results.filter(model_id__exact=selected_model_id)
> >     if selected_gorivo_id != 0:
> >         results.extend(results.filter(gorivo_id__exact=selected_gorivo_id))
> >
> >
> >
> > If I were to code that view I would use a custom manipulator and it
> > would end up something like the following. But PLEASE, PLEASE, be
> > aware that it's just my personal way of doing things, I am no one to
> > tell you how to do things.
> >
> > class FilterResultsManipulator(forms.Manipulator):
> >     def __init__(self):
> >         model_choices = [(m.id, m) for m in Model.objects.all()]
> >         gorivo_choices = [(g.id, g) for g in
> > Gorivo.objects.all().order_by('opis')]
> >         self.fields = (
> >             forms.SelectField(field_name='model', choices=model_choices),
> >             forms.SelectField(field_name='gorivo', choices=gorivo_choices),
> >         )
> >
> > def home(request):
> >     results = []
> >     filter_manipulator = FilterResultsManipulator()
> >     filter_data = {}
> >
> >     if request.POST:
> >         filter_data = request.POST.copy()
> >
> >         model = None
> >         gorivo = None
> >         if filter_data.get('model'):
> >             model = get_object_or_404(Model, pk=filter_data['model'])
> >         if filter_data.get('gorivo'):
> >             gorivo = get_object_or_404(Gorivo, pk=filter_data['gorivo'])
> >
> >         if model:
> >             results = Vozilo.objects.filter(model=model)
> >         if gorivo:
> >             results.extend(Vozilo.objects.filter(gorivo=gorivo))
> >     else:
> >         results = Vozilo.objects.order_by('?')[:5]
> >
> >     filter_form = forms.FormWrapper(filter_manipulator, filter_data, {})
> >
> >     return render_to_response('cars/home.html', {
> >         'results': results,
> >         'filter_form': filter_form,
> >     })
> >
> > And the template you only need to display the form fields.
> >
> > <form method="post" action=".">
> > Search by:
> > <label for="id_model">Model:</label> {{ filter_form.model }}
> > <label for="id_gorivo">Gorivo:</label> {{ filter_form.gorivo }}
> > <input type="submit" name="submit" value="Search" />
> > </form>
> >
> >
> > The custom manipulator and associated form wrapper takes care of
> > displaying the select fields in your form and maintaining the selected
> > option between posted pages. The use of the utility
> > get_object_or_404() frees you of worrying if the incoming data is
> > invalid or malformed, if it is a 404 exception will be raised and the
> > user will get a 404 page.
> >
> > If you have questions about the above code feel free to ask.
> >
> > Cheers,
> > Jorge
> >
> >
> >
> > On 7/13/06, Carlos Yoder <[EMAIL PROTECTED]> wrote:
> > >
> > > Hello people, my ongoing adventures in firstdjangoappland continue thus:
> > >
> > > I'm putting together a simple search app for a car project. On entry,
> > > user sees a list of 5 random cars. He can use up to two comboboxes to
> > > filter the results (car model and fuel type).
> > >
> > > This is what I've come up with on my home() view, so far, and it looks
> > > really ugly.
> > >
> > > def home(request):
> > >
> > >         results = {}
> > >
> > >         # these 2 guys are the filters
> > >         model_list = Model.objects.all()
> > >         gorivo_list = Gorivo.objects.all().order_by('opis')
> > >
> > >         selected_model_id = 0
> > >         selected_gorivo_id = 0
> > >
> > >         if request.POST:
> > >                 # this handles 'SELECTED' attribute on comboboxes
> > >                 try:
> > >                         selected_model_id = int(request.POST['model_id'])
> > >                 except:
> > >                         pass
> > >                 try:
> > >                         selected_gorivo_id = 
> > > int(request.POST['gorivo_id'])
> > >                 except:
> > >                         pass
> > >
> > >                 # and this should take care of the filtering... but I'm 
> > > stumped really
> > >                 # both filters should add up
> > >                 results = Vozilo.objects.all()
> > >                 if selected_model_id != 0:
> > >                         results = 
> > > results.filter(model_id__exact=selected_model_id)
> > >                 if selected_gorivo_id != 0:
> > >                         results = 
> > > results.filter(gorivo_id__exact=selected_gorivo_id)
> > >         else:
> > >         # no POST, we show 5 random cars.
> > >                 results = Vozilo.objects.order_by('?')[:5]
> > >
> > >         return render_to_response('cars/home.html', {
> > >                 'model_list':model_list,
> > >                 'gorivo_list':gorivo_list,
> > >                 'selected_model_id':selected_model_id,
> > >                 'selected_gorivo_id':selected_gorivo_id,
> > >                 'results': results
> > >         })
> > >
> > >
> > > There must be a better way of doing this, right? I'm a terrible newbie
> > > both at Python and Django --and I'm not a kid anymore, so no more time
> > > to spare hitting my head on walls :-)
> > >
> > > Insights? Ideas?
> > >
> > > Thanks a million,
> > >
> > > --
> > > Carlos Yoder
> > > http://carlitosyoder.blogspot.com
> > >
> > > > >
> > >
> >
>
> >
>


-- 
Carlos Yoder
http://carlitosyoder.blogspot.com

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

Reply via email to