2017-11-11 11 views
0

내 프로젝트에는 2 개 이상의 다른 유형의 사용자가 있으며 각 사용자마다 고유 한 사용자 정의 필드와 권한이 있습니다. 인증 방법을 변경하지 않으므로 기본 사용자를 사용하고 프로필 모델을 만들 계획입니다.Django - 2 개 이상의 사용자 유형을 관리하기위한 최선의 방법

내 질문에 각 사용자 유형에 대한 모든 사용자 정의 필드를 포함하는 단일 프로파일 모델을 작성하고 각 사용자에 적합한 필드를 표시하는 것이 더 나은가? (아래의 방법 1 참조) 또는 각 사용자 유형에 대해 별도의 프로필 모델을 만드는 것이 더 좋습니다 (방법 2 참조)?


방법 1

from django.contrib.auth.models import User 

class Profile(models.Model): 
    user = models.OneToOneField(User, ...) 

    usertype1_customfield1 = models.CharField(...) 
    usertype1_customfield2 = models.CharField(...) 
    ... 

    usertype2_customfield1 = models.CharField(...) 
    usertype2_customfield2 = models.CharField(...) 
    ... 

    # and so on for each user type... 

방법 2

from django.contrib.auth.models import User 

class ProfileUserOne(models.Model): 
    user = models.OneToOneField(User, ...) 

    customfield1 = models.CharField(...) 
    customfield2 = models.CharField(...) 
    ... 

class ProfileUserTwo(models.Model): 
    user = models.OneToOneField(User, ...) 

    customfield1 = models.CharField(...) 
    customfield2 = models.CharField(...) 
    ... 

# and so on for each user type... 

답변

0

그 자체로, 각 프로파일의 논리를 분리하도록 허용하는 방법이 바람직하다 수업. user = models.OneToOneField(User, ...)과 같은 일반적인 입력란을 반복하는 것이 염려되는 경우 먼저 앱에 일종의 BaseProfile을 선언 한 다음이를 하위 클래스로 추가 할 수 있습니다.

from django.contrib.auth.models import User 

class BaseProfile(models.Model): 
    user = models.OneToOneField(User, ...) 

class ProfileUserOne(BaseProfile): 

    customfield1 = models.CharField(...) 
    customfield2 = models.CharField(...) 
    ... 

class ProfileUserTwo(BaseProfile): 

    customfield1 = models.CharField(...) 
    customfield2 = models.CharField(...) 
    ... 

# and so on for each user type...