views:

92

answers:

3

I've been able to do this through the django environment shell, but hasn't worked on my actual site. Here is my model:

class ShoeReview(models.Model):
        def __unicode__(self):
                return self.title
        title = models.CharField(max_length=200)
        slug = models.SlugField(unique=True)
        keywords = models.TextField()
        author = models.ForeignKey(User, related_name='reviews')
        pub_date = models.DateTimeField(auto_now_add='true')
        Shoe = models.OneToOneField(Shoe) # make this select those without a shoe review in admin area
        owner_reviews = OwnerReview.objects.filter(Shoe__name=Shoe)

I'm sure this is something silly, but I've been at it for around 6 hours. It's part of the reason I haven't slept in the last 18 :S

I'm trying to return all OwnerReview objects from the OwnerReview Model that match the current Shoe:

class OwnerReview(models.Model):
        def __unicode__(self):
                return self.comments
        name = models.CharField(max_length=50)
        pub_date = models.DateTimeField(auto_now_add='true')
        Shoe = models.ForeignKey(Shoe)
        email = models.EmailField()
        rating = models.PositiveSmallIntegerField()
        comments = models.CharField(max_length=500)

Here is what i'm trying to accomplish done through "python manage.py shell":

>>> from runningshoesreview.reviews.models import Shoe, OwnerReview
>>> ariake = Shoe.objects.get(pk=1)
>>> ariake.name
u'Ariake'
>>> owner_reviews_for_current_shoe = OwnerReview.objects.filter(Shoe__name=ariake.name)
>>> owner_reviews_for_current_shoe
[<OwnerReview: great pk shoe!>, <OwnerReview: good shoes, sometimes tear easy though.>]
A: 

Why is 'Shoe =' capitalized?

Josh
I can change it. It was a mistake, I think, but I don't remember. I'll change it now though.
Codygman
+2  A: 
ariake = Shoe.objects.get(pk=1)
# get the OwnerReviews 
ariake.ownerreview_set.all()
# or the ShoeReviews
akiake.shoereview_set.all()

Or if you really want to use the OwnerReview class directly

OwnerReview.objects.filter(shoe=ariaki)

A question for you. Did you mean to use OnoToOneField(Shoe) and not ForeignKey(Shoe) in the ShoeReview class? ForeignKey makes more sense to me. I would lowercase the 'Shoe' field in both of your review models and remove the reference to OwnerReview in ShoeReview.

class ShoeReview(models.Model):
    # ...
    shoe = models.ForeignKey(Shoe)
    #^^^ lowercase field and ^^^^ capitalized model 
    # VVV remove this next line VVV
    #owner_reviews = OwnerReview.objects.filter(Shoe__name=Shoe)

My example assumes that you have switched from 'Shoe' to 'shoe' for the field name in your models.

istruble
A: 

The reason why you're getting an empty QuerySet is because on your ShoeReview model, your filter argument is wrong:

owner_reviews = OwnerReview.objects.filter(Shoe__name=Shoe)

Change to this:

owner_reviews = OwnerReview.objects.filter(Shoe=Shoe) #without __name or you can do like this also: owner_reviews = OwnerReview.objects.filter(Shoe__name=Shoe.name)

The point is that you can't compare the name attribute to the Shoe object. Either compare an object to an object or an attribute to an attribute.

Luiz C.