Thanks for the help on that portion, it is working wonderfully.

I've been able to power through most of the requests for this project
but there is one outstanding element in regards to pagination.

Here is my view:


def QandAAPI(request, site):
    quest = entry.objects.filter(site__name__icontains=site)
    if 'id' in request.GET:
        ids = request.GET.getlist('id')
        quest = quest.filter(id__in=ids)
    if 'sect' in request.GET:
        sect = request.GET.getlist('sect')
        quest = TaggedItem.objects.get_by_model(quest, sect)
    if 's' in request.GET:
        s = request.GET['s']
        search_query = (
        Q(question__icontains=s) | Q(answer__icontains=s) |
Q(answerer__icontains=s)
        )
        quest = quest.filter(search_query)
    if 'count' in request.GET and not 'page' in request.GET:
        count = request.GET['count']
        quest = quest[:count]
    if 'count' in request.GET and 'page' in request.GET:
        page = int(request.GET['page'])
        count = request.GET['count']
        paginator = Paginator(quest, count)
        try:
            page = paginator.page(page)
        except ValueError:
            page = 1
    json = serializers.serialize("json", quest)
    return HttpResponse(json)

I am getting the following error when I try to plug a page=1 into the
URL

coercing to Unicode: need string or buffer, int found

I'm not entirely sure how to fix this error, I've tried many times.

The final view should do the following:

Check to see if 'page' and 'coutn' are part of the url. If they are,
then pull up the results of that page with the count declared in the
URL.

There will be another component that will skip the 'count' if it isn't
declared but I haven't got there yet.



On May 5, 4:11 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On May 5, 4:46 am, Nick <nickt...@gmail.com> wrote:
>
>
>
> > Here's the deal. I'm working on a customAPIfor moving information
> > about. I am stuck at a point in creating my view.
> > It might be best just to get into the details.
>
> > Here is the model:
>
> > class entry(models.Model):
> >     question = models.CharField('Question', max_length=200,
> > blank=False)
> >     answer = models.TextField('Answer', blank=False)
> >     answerer = models.CharField("Expert's Name", max_length=100,
> > blank=False)
> >     credentials = models.CharField ("Expert's Credentials",
> > max_length=100, blank=False)
> >     published  = models.DateTimeField(auto_now_add=True)
> >     updated = models.DateTimeField(auto_now=True)
> >     site = models.ForeignKey(Site)
> >     section = TagField()
>
> > Once a site is picked the feed is narrowed down by id (whether single
> > id, list or range), then by section (single or multiple) and finally
> > some pagination and count params (I'm not worried about that right
> > now). Currently, I am stuck on the section portion. I may be going
> > about this all wrong. What I would like is for some URL like:
>
> >http://mysite.com/qanda/SITENAME/?sect=THESECTIONNAME
>
> > to produce a JSON output of Questions and Answers under that site with
> > that section name. Here is my view:
>
> > def QandAAPI(request, site):
> >     quest = entry.objects.filter(site__name__icontains=site)
> >     if 'id' in request.GET:
> >         id = request.GET.get('id','')
> >         id = id.split(',')
> >     else:
> >         id = quest.values_list('id', flat=True)
> >     if 'sect' in request.GET:
> >         sect = request.GET.get('sect','')
> >     else:
> >         sect = ''
> >     quest = quest.filter(id__in=id, section=sect)
> >     object = serializers.serialize("json", quest)
> >     json = "%s" % object
> >     return HttpResponse(json)
>
> > Basically, I would like the "else: sect=" to be a WILDCARD so if the
> > sect portion of theAPIisn't explicitly stated then it just pulls
> > everything.
>
> > What do you think? Raw SQL? I'm a complete bonehead? All opinions/
> > advice are welcome.
>
> I wouldn't call you a bonehead, but the method you're using to get all
> the objects when no id is passed in is... well... special.
>
> The key to doing this sort of filtering is to remember that QuerySets
> are lazy, and successive filters can be applied to them without
> hitting the database, until the time comes to actually evaluate them.
> So what you want to do is to set up the base query at the start of the
> method, then further filter it depending on the GET parameters. Then,
> if a particular parameter isn't passed in, you simply don't filter on
> it. Something like:
>
> def QandAAPI(request, site):
>     quest = entry.objects.filter(site__name__icontains=site)
>     if 'id' in request.GET:
>         ids = request.GET.getlist('id')
>         quest = quest.filter(id__in=ids)
>     if 'sect' in request.GET:
>         sect = request.GET['sect']
>         quest = quest.filter(section=sect)
>     json = serializers.serialize("json", quest)
>     return HttpResponse(json)
>
> (Also note that I'm using getlist for the 'id' parameter - that
> enables you to get multiple values from GET parameters, when they are
> supplied in the form /myurl/?id=1&id=2&id=3, which is the way browsers
> automatically do it when submitting forms (rather than ?id=1,2,3).)
> --
> DR.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

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

Reply via email to