Issue
I have the following model:
from django.db import models
from tvproject_api.models import TVUser
class Watched(models.Model):
tvuser = models.ForeignKey(TVUser, on_delete=models.CASCADE, related_name='watched_tvuser')
show_id = models.IntegerField()
season_id = models.IntegerField()
date_added = models.DateTimeField()
class Meta:
unique_together = [['tvuser', 'show_id', 'season_id']]
I'm trying to annotate the TVUser model with a count of this by doing,
TVUser.objects.filter().annotate(watched_count=Count('watched_tvuser'))
But this gives me,
django.core.exceptions.FieldError: Cannot resolve keyword 'watched_tvuser' into field.
Weirdly, I have another model that looks almost exactly like the Watched
model:
from django.db import models
from tvproject_api.models import TVUser
class Watchlist(models.Model):
tvuser = models.ForeignKey(TVUser, on_delete=models.CASCADE, related_name='watchlist_tvuser')
show_id = models.IntegerField()
season_id = models.IntegerField()
date_added = models.DateTimeField()
class Meta:
unique_together = [['tvuser', 'show_id', 'season_id']]
And this works totally fine:
TVUser.objects.filter().annotate(watched_count=Count('watchlist_tvuser'))
Not sure what the problem here is. I'm able to annotate lots of different fields, but the one for watched_tvuser
is the only one not working.
Solution
It turned out I wasn't exporting the Watched model from my models/init.py file
Answered By - Human Cyborg Relations
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.