我有两个类似的Django模型:
class Skill(models.Model):
title = models.CharField(max_length=255)
def __unicode__(self):
return self.title
class UserSkills(models.Model):
user = models.ForeignKey(User)
skill = models.ManyToManyField(Skill)
def __unicode__(self):
return '%s | %s' % (self.user, self.skill)
现在,我有与用户相关的技能列表,我想同时更新技能模型和用户的相关技能。类似的东西:
# cleaned_skills[] is list if unicode strings
for skill in cleaned_skills:
s, created = Skill.objects.get_or_create(title=skill)
s.save()
u, created = UserSkills.objects.get_or_create(skill=s, user=request.user)
u.save()
出于某种原因,这对我来说并不合适。我是否应该同时更新Skill
和User
模型?
答案 0 :(得分:1)
就个人而言,我认为你正确地做到了。