2015-02-06 78 views
0

什麼是使用django和postgresql設計多級用戶登錄系統的最佳方式。用戶的詳細信息是頭(管理員),學生,教師,工作人員等。這些不同類型的用戶詳細信息具有不同的字段,我們不能更改該字段。我們如何通過組合所有這些類型的用戶來設計用戶模型。Django多級用戶處理

class Heads(models.Model): 
    gid  = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=50) 
    emp_code= models.CharField(max_length=50) 
    school = models.ForeignKey(SchoolDetails) 
    prdFrom = models.DateField() 
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    designation=models.CharField(max_length=50) 
    address =models.TextField() 
    def __unicode__(self): 
     return unicode(self.name) 
    class Meta: 
     db_table = u'heads' 
     verbose_name = "heads" 

class Student(models.Model): 
    gid  = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=50) 
    stud_code= models.CharField(max_length=50) 
    school = models.ForeignKey(SchoolDetails) 
    std = models.IntegerField() 
    division=models.CharField() 
    parents_email_id=models.CharField(max_length=50) 
    parents_contact_no=models.CharField(max_length=50) 
    addess =models.TextField() 
    def __unicode__(self): 
     return unicode(self.name) 
    class Meta: 
     db_table = u'students' 
     verbose_name = "students"  

class Teacher(models.Model): 
    gid  = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=50) 
    emp_code= models.CharField(max_length=50) 
    school = models.ForeignKey(SchoolDetails) 
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    address =models.TextField() 
    is_lead= models.CharField() 
    def __unicode__(self): 
     return unicode(self.name) 
    class Meta: 
     db_table = u'teacher' 
     verbose_name = "teacher"   

class Staff(models.Model): 
    gid  = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=50) 
    emp_code= models.CharField(max_length=50) 
    school = models.ForeignKey(SchoolDetails) 
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    address =models.TextField() 
    designation= models.CharField() 
    def __unicode__(self): 
     return unicode(self.name) 
    class Meta: 
     db_table = u'staff' 
     verbose_name = "staff"    

請給出答案。

謝謝。

+0

請給出代碼。謝謝。 – 2015-02-06 05:10:55

+0

Dear Lego Stormtroopr,我編輯了我的問題... – Anju 2015-02-06 06:19:06

回答

0

你可以extend the User model與您的所有型材狀類的,所以也就是這將給你:

class Heads(models.Model): #<-- by convention you should use singular 'Head' 
    user = models.OneToOneField(User) 
    <other Head-specific fields not included in User> 

所以User型始終是相同的,但附加的配置文件是不同的。如果在示例中使用OneToOneField,則在向後關係中,您只需檢查哪一個不是None即來自User -instance至正確的Profile -instance。

當然,根據用戶所屬的配置文件,您可能仍然會有不同的用於創建用戶的窗體或方法。

如果您對Django默認的用戶模型所保留的信息不滿意,您還可以更進一步,並保留所有配置文件共有的所有信息。