class Album:
songs = models.ManyToManyField(Song)
class Song:
title = models.CharField()
ratings = GenericRelation(Rating)
modified_at = models.DateTimeField(default=timezone.now, db_index=True)
class Rating:
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey('content_type', 'object_id')
time_stamp = models.DateTimeField(auto_now=True, db_index=True)
对于用户,我想列出相册中的歌曲。
我想在用户评价歌曲后显示每首歌曲是否已被修改。
在代码中,
if Rating.objects.filter(user=user,
content_type=ContentType.objects.get_for_model(song), object_id=song.id,
time_stamp__lt=song.modified_at).exists():
# song has been modified after user has rated it
此刻我有以下内容,查询失败并显示错误
Cannot resolve keyword 'content_type' into field. Choices are: title, ratings, modified_at
我认为content_type
隐含了content_type
错误所指的ratings = GenericRelation(Rating)
。
qs = album.songs.all()
album.songs.prefetch_related(
Prefetch(
'ratings',
queryset=qs.filter(
ratings__user=user,
ratings__rating_type=RATING_TYPE_VIEW
).annotate(time_stamp=Max('ratings__time_stamp')),
to_attr='ratings_prefetched'
)
)
我在django 1.8.4