我正在尝试重构我的小投票功能。我有两个几乎完全相同,只是针对不同的模型字段。如果选民喜欢一首歌,他们可以给它加星,如果他们不喜欢一首歌,他们可以给它加标记。然后选民通过 many2many 关系与歌曲相关联,因此可以确定他们是否已经对该歌曲进行了投票。如果是这样,则从歌曲中删除星号(或旗帜),并且也删除了选民与歌曲之间的关系。
这是添加或删除星星的函数:
# models.py
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True, related_name = 'profile')
liked_songs = models.ManyToManyField('Song', blank = True, null = True)
disliked_songs = models.ManyToManyField('Song', blank = True, null = True)
class Song(models.Model):
song_id = models.CharField(max_length = 36, primary_key = True)
last_accessed = models.DateTimeField()
stars = models.IntegerField(max_length = 5, default = 0)
flags = models.IntegerField(max_length = 5, default = 0)
# views.py
def vote(request, song_id, task): # 'task' should be either 'stars' or 'flags'
song = Song.objects.get(song_id = song_id)
voter = request.user.get_profile()
# Voter does not already like the song
if not voter.liked_songs.filter(song_id = song.song_id): # (*)
try:
# Increase vote, set 'last_accessed', add Song to UserProfile.liked_songs
Song.objects.filter(song_id = song_id).update(
stars = F('stars') + 1, # (*)
last_accessed = datetime.datetime.now()
)
voter.liked_songs.add(song) # (*)
except:
raise
else:
try:
# Decrease vote, set 'last_accessed', remove Song from UserProfile.liked_songs
Song.objects.filter(song_id = song_id).update(
stars = F('stars') - 1, # (*)
last_accessed = datetime.datetime.now()
)
voter.liked_songs.remove(song) # (*)
except:
raise
return "Done."
添加或删除标志的功能完全相同,除了标记的 (*) 行:
if not voter.disliked_songs.filter(song_id = song.song_id)
flags = F('flags') + 1,
flags = F('flags') - 1,
voter.disliked_songs.add(song)
voter.disliked_songs.remove(song)
现在这是我的第一个问题:我必须做什么才能只使用一个功能来喜欢和不喜欢一首歌?我已经介绍了参数task
,但我似乎不能写task = F('task') + 1,
,因为 Song 模型中没有字段名称“任务”。
stars = F('stars') + 1,
奖励问题:和之间的唯一区别stars = F('stars') - 1,
是运算符(+
或-
)。有没有办法在变量中使用运算符,即task = F('task') myoperator 1,
?