I am trying to define a proxy model for my admin view and have it only show objects that have a certain amount of foreign key values.
This is what I’m trying, but I’m not able to filter on yVotes:
class Post(models.Model):
title = models.CharField(max_length=512)
class PostVote(models.Model):
post = models.ForeignKey(Post)
vote = models.CharField(max_length=1)
class VotedPost(models.Post):
def _yVotes(self):
return models.PostVote.objects.filter(post=self, vote='Y').count()
yVotes = property(_yVotes)
class Meta:
proxy = True
class VotedPostAdmin(PostAdmin):
list_display = ('title', 'yVotes')
def queryset(self, request):
return self.model.objects.filter(yVotes__gt=0)
So my end result would be when you browse to /admin page for VotedPost, it would only show posts that have more than 0 ‘Y’ votes. Commenting out the queryset in VotedPostAdmin, the proper value for yVotes will display in the list_display.
Thanks in advance!
Here is the solution I’ve ended up with so far to get me the results I need. If anyone has a more elegant solution I will wait to accept: