0

我有两个这样的 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()

出于某种原因,这对我来说感觉不对。有什么方法可以同时更新模型SkillUser模型吗?

4

1 回答 1

1

就个人而言,我认为你做得对。

于 2013-10-03T23:25:47.330 回答