Skip to content

Instantly share code, notes, and snippets.

@makmac213
Created October 10, 2013 06:03
Show Gist options
  • Save makmac213/6913751 to your computer and use it in GitHub Desktop.
Save makmac213/6913751 to your computer and use it in GitHub Desktop.
For random multiple classes query (ala pinterest)
import os
from django.conf import settings
from django.shortcuts import HttpResponse
from django.template import RequestContext
from bar.models import Bar
from foo.models import Foo
def get_baz_ajax(request):
"""
Returns a combined random list of foos and bars
"""
if request.method == 'POST':
page = request.POST.get('page', 1)
foo_offset = (int(page)-1) * settings.NUM_FOO_RECORDS
bar_offset = (int(page)-1) * settings.NUM_BAR_RECORDS
foo = Foo.objects.filter()[foo_offset:settings.NUM_FOO_RECORDS + foo_offset]
bar = Bar.objects.filter()[bar_offset:settings.NUM_BAR_RECORDS + bar_offset]
baz = list(foo) + list(bar)
# shuffle the baz to make random list
random.shuffle(baz)
html = render_to_string('interface/list_items.html',
{'baz':baz},
RequestContext(request))
return HttpResponse(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment