2015-02-06 75 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默认的用户模型所保留的信息不满意,您还可以更进一步,并保留所有配置文件共有的所有信息。