2011-06-11 4 views
4

나는 userprofile에 사용자 교육을 추가하고 있습니다. 사용자는 자신의 교육에 대해 여러 항목을 가질 수 있습니다. 같은 기본 M2M 관계를 이용하여 I이어야한다 -M2M 관계에서 통과 모델 사용 여부

class Education(models.Model): 
    school = models.CharField(max_length=100) 
    class_year = models.IntegerField(max_length=4, blank=True, null=True) 
    degree = models.CharField(max_length=100, blank=True, null=True) 

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    educations = models.ManyToManyField(Education) 

아니면이 관계 모델을 통해를 사용 하는가? 고맙습니다.

답변

2

@manji이 맞습니다. Django는 through을 사용하는지 여부에 관계없이 매핑 테이블을 만듭니다. 당신은 특정 교육이 사람 최종 학교를 대표 여부를 추적 할 through 테이블의 필드를 가질 수
:

는 중간에 더 많은 필드를 추가하거나 through 테이블하려는 이유의 예를 제공하기 위해 출석자 :

class Education(models.Model): 
    ... 

class UserProfile(models.Model): 
    ... 
    educations = models.ManyToManyField(Education, through='EduUsrRelation') 

class EducationUserRelation(models.Model): 
    education = models.ForeignKey(Education) 
    user_profile = models.ForeignKey(UserProfile) 
    is_last_school_attended = models.BooleanField() 
2

장고 create automatically an intermediary은 두 모델 간의 ManyToMany 관계를 나타냅니다.

이 테이블에 필드를 추가하려면 through 속성을 통해 자신의 테이블 (예 : 모델)을 제공하십시오. 그렇지 않으면 필요하지 않습니다.