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:
这是模型。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 个解决方案
#1
1
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()
#2
1
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.
owner_review__ratings__rating_attribut预期模型(它将从模型中检索pk并在查询中使用它)。还有order_by方法,不是sort_by。在这个查询的旁边似乎很好。试着运行它,告诉你,如果你得到了结果或者有一些错误。尝试运行它。/管理。py shell,这使得检查它是否有效变得简单和快速。
#1
1
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()
#2
1
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.
owner_review__ratings__rating_attribut预期模型(它将从模型中检索pk并在查询中使用它)。还有order_by方法,不是sort_by。在这个查询的旁边似乎很好。试着运行它,告诉你,如果你得到了结果或者有一些错误。尝试运行它。/管理。py shell,这使得检查它是否有效变得简单和快速。