帮助我的鞋子评论网站上的django模特

时间:2010-03-03 22:06:29

标签: python django database-design django-models

我现在想要实现的目标:

  • 允许我添加新的鞋子对象和鞋子审查对象。 完整
  • 允许每只鞋上的“所有者评论”完成(我想,但我的模型中可能会有某些地方可以改进)
  • 允许对我定义的属性进行评级,这些属性与特定鞋子相关联。优选地,具有迭代与特定鞋相关的所有评级字段的简单方式。
  • 我的总体目标是能够轻松地推荐具有与用户过去评级相似的属性等级的鞋子。然后允许这些鞋子评论按品牌,价格,基于属性评级的高/低排序进行过滤,以及使用基于活动(标签)鞋子的这些排序选项。

我觉得我很亲密,但却缺少一些我需要的关系。能够使用以下内容获得高评价的鞋子评论会非常方便。

ShoeReview.objects.filter(owner_review__ratings__rating_attribute = 'overall').sort_by(owner_review__ratings__rating)

我认为以上是正确的?同样,对django来说是新的,所以请原谅任何愚蠢的错误。和往常一样,在我问这里之前,我已经用我的代码破解了几天,所以不要以为我只是在没有自己付出努力的情况下发布了它。

以下是models.py文件内容:

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

2 个答案:

答案 0 :(得分:1)

而不是:

ShoeReview.objects.filter(owner_review__ratings__rating_attribute = 'overall').sort_by(owner_review__ratings__rating)
你应该致电:

ShoeReview.objects.filter(owner_review__ratings__rating_attribute__attribute = 'overall').order_by(owner_review__ratings__rating)

owner_review__ratings__rating_attribut期望模型(它将从模型中检索pk并在查询中使用它)。还有order_by方法,而不是sort_by。除了那个查询似乎没问题。尝试运行它,并告诉您,如果您得到结果或有一些错误。尝试在./manage.py shell中运行它,这样可以轻松快速地检查它是否有效。

答案 1 :(得分:1)

你应该这样做:

ShoeReview.objects\
          .filter(owner_review__ratings__rating_attribute__attribute='overall')\
          .order_by(owner_review__ratings__rating)

但实际上,你最好使用经理:

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)

这样你就可以:

class ShoeReview(models.Model)

    objects = models.Manager() # The default manager.
    best = BestShoesReviewManager() # The best review manager.

在您的代码中,您可以这样做:

ShoeReview.objects.best()