在Django中注释多对多关系
问题描述:
我有两个模型User和VideoData。这两个模型包含每个用户的详细信息以及该用户观看的视频。我使用了多对多的关系。现在我想按用户数量筛选前10名视频。在Django中注释多对多关系
class User(models.Model):
user_id = models.CharField(max_length=40,unique=True)
user_name = models.CharField(max_length=40)
user_email = models.EmailField()
user_city = models.CharField(max_length=40)
videos_watched = models.ManyToManyField('VideoData', through='WatchedVideo')
class Meta:
ordering = ['user_id']
verbose_name = 'User MetaData'
verbose_name_plural = 'Users MetaData'
def __unicode__(self):
return self.user_id
class VideoData(models.Model):
video_id = models.CharField(max_length=40,unique=True)
video_name = models.CharField(max_length=40)
class Meta:
verbose_name = 'User_Video MetaData'
verbose_name_plural = 'Users_Video MetaData'
def __unicode__(self):
return self.video_name
class WatchedVideo(models.Model):
user = models.ForeignKey(User, to_field = 'user_id')
videoData = models.ForeignKey(VideoData, to_field = 'video_id')
time = models.PositiveIntegerField(null = True)
我该如何注释前10个视频和观看这些视频的用户数?从Django的ORM
什么是'time'在'WatchedVideo'?这是用户观看视频的次数吗? – cezar
@cezar没有时间观看时间,即视频观看了多少小时。 – Naresh
只是对编码约定的一个评论:最好将'WatchedVideo'中的'videoData'重命名为'video_data'以符合其余的。 – cezar