我有两个这样的 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
模型吗?