2017-06-17 40 views
0

我有一個PatientDoctor模型,從Django的用戶繼承獲取RelatedManager而不是讓模型對象的ID

class HmsUser(AbstractBaseUser, PermissionsMixin): 
    username = models.CharField(_('username'), max_length=30, unique=True, 
     help_text=_('Required. 30 characters or fewer. Letters, numbers and ' 
        '@/./+/-/_ characters'), 
     validators=[ 
      validators.RegexValidator(re.compile('^[\[email protected]+-]+$'), _('Enter a valid username.'), 'invalid') 
     ]) 
    email = models.EmailField(_('email address'), blank=True) 
    is_staff = models.BooleanField(_('staff status'), default=False, 
     help_text=_('Designates whether the user can log into this admin ' 
        'site.')) 
    is_active = models.BooleanField(_('active'), default=True, 
     help_text=_('Designates whether this user should be treated as ' 
        'active. Unselect this instead of deleting accounts.')) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 

    objects = UserManager() 

    USERNAME_FIELD = 'username' 
    REQUIRED_FIELDS = ['email'] 

    def __unicode__(self): 
     return self.get_full_name(); 

    def get_profile(self): 
     # .... 

    def real(self,): 
     try: return self.doctor 
     except Doctor.DoesNotExist: 
      try: return self.operator 
      except Operator.DoesNotExist: 
       try: return self.patient 
       except Patient.DoesNotExist: 
        try: return self.organization 
        except Organization.DoesNotExist: 
         return self 

    def is_doctor(self): 
     return self.real().__class__ == Doctor 

    def is_patient(self): 
     return self.real().__class__ == Patient 

    def is_operator(self): 
     return self.real().__class__ == Operator 

    def is_organization(self): 
     return self.real().__class__ == Organization 

    def really(self, user_type): 
     return isinstance(self.real(), user_type) 

class Person(HmsUser): 
    GENDER_CHOICES = (
     ('M', 'Male'), 
     ('F', 'Female'), 
    ) 
    first_name = models.CharField(_('first name'), max_length=30, blank=True) 
    last_name = models.CharField(_('last name'), max_length=30, blank=True) 
    dob  = models.DateField('Date Of Birth') 
    sex  = models.CharField(max_length=1, choices=GENDER_CHOICES) 
    address = models.CharField(max_length=300, blank=True) 
    image  = models.ImageField(upload_to=rename_image, null=True, blank=True) 

    class Meta: 
     abstract = True 
    # .... 

class Patient(Person): 
    def get_full_name(self): 
     return '%s. %s' % (('Mr', 'Ms')[self.sex == 'F'], super(Patient, self).get_full_name()) 

    def get_short_name(self): 
     return '%s. %s' % (('Mr', 'Ms')[self.sex == 'F'], super(Patient, self).get_short_name()) 

    def admission(self): 
     admissions = self.admissions.filter(released = None) 
     return admissions.count() > 0 and admissions.latest('admitted') or None 

    def doctors(self): 
     id_list = self.stories.values_list('doctor', flat=True).distinct() 
     return Doctor.objects.filter(pk__in=set(id_list)) 

    def visits(self): 
     # .... 

    def profile(self): 
     return reverse('patient', args=(self.id,)) 

class Doctor(Person): 
    paramedic  = models.BooleanField() 
    specialization = models.CharField(_('Specialization'), max_length=64, blank=True) 
    organizations = models.ManyToManyField(Organization, through='Membership', blank=True) 

    def get_full_name(self): 
     return '%s. %s' % ('Dr', super(Doctor, self).get_full_name()) 

    def get_short_name(self): 
     return '%s. %s' % ('Dr', super(Doctor, self).get_short_name()) 

    def stories(self): 
     # .... 

    def patients(self): 
     id_list = self.stories.values_list('patient', flat=True).distinct() 
     return Patient.objects.filter(pk__in=set(id_list)) 

    def profile(self): 
     return reverse('doctor', args=(self.id,)) 

我對醫生和病人模型

def patient(request, patient_id): 
    patient = Patient.objects.get(id=patient_id) 
    print(patient.__class__)  # <class 'identity.models.Patient'> 
    print(patient.real().__class__) # <class 'django.db.models.fields.related_descriptors.RelatedManager'> 

def doctor(request, doctor_id): 
    doctor = Doctor.objects.get(id=doctor_id) 
    print(doctor.__class__)  # <class 'identity.models.Doctor'> 
    print(doctor.real().__class__) # <class 'identity.models.Doctor'> 

doctor.real().__class__和不同的看法doctor.__class__ return <class 'identity.models.Doctor'>

但是

patient.real().__class__返回<class 'django.db.models.fields.related_descriptors.RelatedManager'>

然而

patient.__class__回報<class 'identity.models.Patient'>

爲什麼patient.real().__class__<class 'identity.models.Patient'>

當我打開Django的shell

In [15]: d = Doctor.objects.get(id=68)  
In [16]: p = Patient.objects.get(id=69)  
In [17]: d.doctor 
Out[17]: <Doctor: Dr. Shatadal Saha>  
In [18]: p.patient 
Out[18]: <django.db.models.fields.related_descriptors.RelatedManager at 0x7fd86e45ee90> 

回答

-1

當您打印patient.real().__class__這個。

所以這從具有ForeignkeyManytoMany與其他模特實方法返回一個字段。所以patient.real().__class__ print字段關係類別不是自我類名稱

+0

所以這意味着通過一些'related_name'一個'Patient'與另一個'Patient'關聯。但Patient類沒有聲明任何這樣的相關名稱。還是我忽略了?我如何確定相關領域的來源是什麼? –

+0

但你繼承超級HmsUser類有這種類型的字段 –

+0

所以這也應該適用於醫生。因爲Doctor也繼承了HmsUser –

相關問題