Reputation: 832
What i'm trying to achieve at this point:
I feel like i'm close, but missing something about the relationships I need. It would be very convenient to be able to use the following to get shoe reviews with a high rating.
ShoeReview.objects.filter(owner_review__ratings__rating_attribute = 'overall').sort_by(owner_review__ratings__rating)
I think the above is correct? Again, new-ish to django so please forgive any silly mistakes. As always, i've hacked around with my code for a few days before asking here, so don't think I just posted this up without putting forth my own effort.
Here are the models.py file contents:
from django.db import models
from django.contrib.auth.models import User
from tagging.fields import TagField
class Brand(models.Model):
def __unicode__(self):
return self.name
name = models.CharField(max_length=200)
popularity = models.PositiveIntegerField(default=0)
class Shoe(models.Model):
def __unicode__(self):
return self.name
brand = models.ForeignKey(Brand)
name = models.CharField(max_length=200)
description = models.TextField()
price = models.PositiveIntegerField()
buylink = models.URLField(verify_exists='true', default="http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2FAthletic-Outdoor%2Fb%3Fie%3DUTF8%26node%3D679564011%26ref_%3Damb%5Flink%5F23091522%5F13&tag=runnshoerevi-20&linkCode=ur2&camp=1789&creative=390957", max_length=400)
picture = models.ImageField(upload_to='shoes', blank=True, default="shoes/default_picture.jpg")
youtube_video_id = models.CharField(max_length=25, blank=True)
class RatingAttributes(models.Model):
def __unicode__(self):
return self.attribute
attribute = models.CharField(max_length=65)
class Rating(models.Model):
def __unicode__(self):
return "%s, %s: %s" % (str(self.author), str(self.rating_attribute), str(self.rating))
rating_attribute = models.ForeignKey(RatingAttributes)
author = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now_add='true')
rating = models.IntegerField(blank=True, null=True)
class OwnerReview(models.Model):
def __unicode__(self):
return self.comments
author = models.ForeignKey(User)
pub_date = models.DateTimeField(auto_now_add='true')
shoe = models.ForeignKey(Shoe)
youtube_video_id = models.CharField(max_length=25, blank=True)
comments = models.TextField()
# this field will be increased every time a user clicks "this review was helpful and all reviws will be sorted by the helpfulness value
ratings = models.ForeignKey(Rating)
helpfulness = models.IntegerField(default=0, blank=True)
class ShoeReview(models.Model):
def __unicode__(self):
return self.title
pub_date = models.DateTimeField(auto_now_add='true')
author = models.ForeignKey(User, related_name='reviews')
title = models.CharField(max_length=200)
keywords = models.CharField(max_length=800, blank=True, default="Shoe Review")
slug = models.SlugField(unique=True)
cur_shoe = models.ForeignKey(Shoe)
staff_opinion = models.TextField()
owner_review = models.ForeignKey(OwnerReview)
shoe_activities = TagField()
Upvotes: 1
Views: 1018
Reputation: 597063
You should do:
ShoeReview.objects\
.filter(owner_review__ratings__rating_attribute__attribute='overall')\
.order_by(owner_review__ratings__rating)
But actually, you are better off using a manager:
class BestShoesReviewManager(models.Manager):
def get_query_set(self):
qs = super(DahlBookManager, self).get_query_set()
return qs.filter(owner_review__ratings__rating_attribute__attribute= 'overall')\
.order_by(owner_review__ratings__rating)
This way you can do:
class ShoeReview(models.Model)
objects = models.Manager() # The default manager.
best = BestShoesReviewManager() # The best review manager.
And so in your code, you can do this:
ShoeReview.objects.best()
Upvotes: 1
Reputation: 42198
Instead of that:
ShoeReview.objects.filter(owner_review__ratings__rating_attribute = 'overall').sort_by(owner_review__ratings__rating)
you should call:
ShoeReview.objects.filter(owner_review__ratings__rating_attribute__attribute = 'overall').order_by(owner_review__ratings__rating)
owner_review__ratings__rating_attribut
expects model (it will retrieve pk from the model and use it in the query). Also there is order_by method, not sort_by. Beside that query seems fine. Try running it and tell, if you get the results or there are some errors. Try running it in ./manage.py shell
, this makes it easy and quick to check if it works.
Upvotes: 1