具有更多属性的 M2M django

M2M django with more attributes

我对使用 django 的 M2M 有点困惑,我有以下问题:

每个用户可以拥有太多技能,每个技能用户必须选择体验时间。我的意思是,django-1 年,Heroku-3 年,等等,我如何在 django 中实现它?

我的模特:

class Specialities(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name


class Experience(models.Model):
    specialities = models.ManyToManyField('self',through='RegisterProfessional',symmetrical = False)
    years = models.CharField(max_length=100)

    def __unicode__(self):
        return self.years

class RegisterProfessional(models.Model):
    id_document = models.OneToOneField(User)
    specialities = models.ForeignKey(Specialities)
    anios = models.ForeignKey(Experience)

我怎样才能修复我的模型来实现这个目标?

我会尽量简化:

class Speciality(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name


class User(models.Model):
    specialities = models.ManyToManyField('self',through='RegisterProfessional',symmetrical = False)


class RegisterProfessional(models.Model):
    id_document = models.ForeignKey(User)
    speciality = models.ForeignKey(Speciality)
    years = models.CharField(max_length=100)

    class Meta(object):
        unique_together = ('id_document', 'speciality')

对于用户,如果您指的是 django 身份验证用户,there is a way to extend it easily 能够直接向 User 模型添加专业。