django orm:使用一个基本模型连接2个模型

时间:2018-07-23 07:30:04

标签: python django orm

我有3个模型auth-user作为基本模型,其他2个模型的外键为auth-user。现在,我想合并所有三个表并创建一个对象,该对象将从合并的3个表中获取所有数据。

class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=150)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'


    class employees(models.Model):
        employee_name = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True, to_field='username')
        department = models.CharField(max_length=100)
        dob = models.DateField()
        doj = models.DateField()
        blood_grp = models.CharField(max_length=100, null=True)
        father_name = models.CharField(max_length=100, null=True)
        add1 = models.CharField(max_length=100, null=True)
        add2 = models.CharField(max_length=100, null=True)
        # doj = models.ImageField(upload_to='profile_pics', blank=True)
        con_relation = models.CharField(max_length=100)
        # email_id = models.EmailField(max_length=254, blank=True, null=True)
        webmail_id = models.CharField(max_length=100, blank=True, null=True)
        emergency_mob = models.CharField(max_length=100)
        contact_person = models.CharField(max_length=100, blank=True, null=True)
        biometric = models.CharField(max_length=100, blank=True, null=True)
        biometric_id = models.CharField(max_length=100, blank=True, null=True)
        offer_letter = models.CharField(max_length=100, blank=True, null=True)
        team_leader = models.CharField(max_length=100)
        manager = models.CharField(max_length=100, blank=True, null=True)
        employment_status = models.CharField(max_length=100, blank=True, null=True)
        company_id = models.CharField(max_length=100)
        adhar_id = models.CharField(max_length=100, blank=True, null=True)
        pan = models.CharField(max_length=100, null=True)



    class auth_ext(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True, to_field='username')
        emp_code= models.CharField(max_length=100,unique=True,default=True) 
        is_role = models.CharField(max_length=100)
        is_designation = models.CharField(max_length=100)
        profile_image = models.ImageField(upload_to='profile_pics', blank=True)
        mobile_no = models.CharField(max_length=100)
        # add_skill = models.CharField(max_length=100, blank=True, null=True)
        class meta(object):
            """docstring for meta"""
            def __init__(self, arg):
                super(meta, self).__init__()
                self.arg = arg

views.py

def Employees(request):
    # emp = employees.objects.all()
    emp = employees.objects.select_related()
    # return render('hr/employees.html', locals(),    context_instance=RequestContext(request))
    # emppp = employees.objects.select_related().filter(foreign_record__foreign_attribute__gt=foo)
    return render(request, 'hr/employees.html',{'emp':emp})

hr/employees.html

{% for emp in emp %}
<tr>
    <td>
        <a href="" class="avatar">J</a>
        <h2><a href="">John Doe <span>Web Designer</span></a></h2>
    </td>
    <td>{{emp.dob}}</td>
    <td>{{emp.User.email}}</td>
    <td>9876543210</td>
    <td><button class="btn btn-xs btn-primary" value="Web Developer">Web Developer</button></td>

0 个答案:

没有答案