I’ve got two models like this (simplified):
class Post(models.Model):
content = models.TextField('content')
#...
class Vote(models.Model):
user = ForeignKey(User)
status = models.TextField(choices = some_choices, default = 'likes')
post = models.ForeignKey(Post)
#...
What i want to do is to select (using one query) posts using some filter with one particular (let’s say current) user’s votes on this posts (it’s okay if he didn’t vote for it), so I can then output all posts and the user can see which ones he liked, which ones he didn’t and which ones he didn’t vote at all.
select_related for Vote model will not help here, because related objects cannot be filtered, so I guess I should do something with extra, but I cannot figure out what arguments should I pass.
So I guess, it should be something like:
Post.objects.filter(content__contains="test").extra(tables="app_vote", where={'my_vote_status': 'something here perhaps?'})
Could you please help me to understand how make a query like this?
UPD: schacki provided a good solution, the only problem is that I want to access votes by different users from the template, somtehing like Post.vote_by_me and Post.vote_by_this_user or Post.vote_by_top_user
Well, If you proper want answers, sometimes you should look for them yourself 🙂
Here’s how I’ve solved my problem:
UPD: probably would work without
tablesargument