views:

38

answers:

1

What i'm trying to achieve at this point:

  • Allow me to add new shoe objects, and shoe review objects. complete
  • Allow "Owner Reviews" on each shoe complete (I think, but there might be somewhere in my model I can improve)
  • Allow ratings of attributes defined by me which are linked to a particular shoe. Preferably, having an easy way to iterate all of the rating fields related to the particular shoe.
  • My overall goal is to have an easy way to be able to suggest shoes with similar attribute ratings to those that the user have rated in the past. Then allowing those shoe reviews to be filtered by brand, price, high/low sorting based on attribute rating, as well as use these sorting options based on activities(tags) shoes are used for.

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()
A: 

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.

gruszczy