0

I want to display related objects to the one a user checked out on my django site. Just like recommendation. For example, When a user click on an object in a state in Las vegas, I want other related objects in Las vegas to display by the sidebar.

Just like when a user click on a link called "home in Las vegas", and when the user is redirected to a page that display the home, and on the sidebar it display "Other homes in Las Vegas" Hope you get my point? I tried the below codes, but it's not working. Been fighting with this all day, yet no success.

Models

class Finhall(models.Model):
    user=models.ForeignKey(User)
    name=models.CharField(max_length=250, unique=True)
    address=models.CharField(max_length=200)
    city=models.CharField(max_length=200)
    state=models.CharField(max_length=200, help_text='Las vegas')

    def __unicode__(self):
        return u'%s' % (self.name)

Views:

def homedetail(request,finhall_id,slug):
    post=Finhall.objects.get(id=finhall_id,slug=slug) #show details of an object

    stateme=Finhall.objects.get(state)  #show similar objects based on state
    booms=Finhall.objects.filter(state=stateme)
    vips=booms.select_related()
    for vip in vips:
        print vip.id
    return render_to_response('postdetail.html',{'post':post,'vips':vips,'Finhall':Finhall},context_instance=RequestContext(request))
picomon
  • 1,479
  • 4
  • 21
  • 38

1 Answers1

1

Assuming state derives from post you should be using filter instead of get and iterate stateme in your template, e.g.

def home(request, finhall_id, slug):
    qs = Finhall.objects.all()

    try:
        finhall = qs.get(id=finhall_id, slug=slug)
    except Finhall.DoesNotExist:
        finhall = None

    if finhall:
        similar_finhalls = qs.filter(finhall.state)
    else:
        similar_finhalls = Finhall.objects.none()

    # other stuff

    return render_to_response('home.html', {
        'finhall': finhall,
        'similar_finhalls': similar_finhalls
    },context_instance=RequestContext(request))
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100