简体   繁体   中英

django query compare two models

I have three models, I want to display users who apply for leave but I don't know how to accomplish that. The relationship between user to new leave is one to many and the leave balance to new leave is many to many and user to balance is one to one. I only want to display data from user and new leave.

 class newleave(models.Model):
    user=models.ForeignKey(User,default='',on_delete=models.CASCADE)
    leave_balance=models.ManyToManyField(Leave_Balance)
    leave=(
            ('annual','annual'),
            ('sick','sick'),

        )

    Leave_type=models.CharField(max_length=100,choices=leave,blank=False,default='')
    dp=(
        ('test','test'),
        ('test1','test1'),

    )

    department=models.CharField(max_length=100,choices=dp,blank=False,default='')
    Start_Date=models.DateField(null=True, blank=False, default=None)
    End_Date=models.DateField(null=True, blank=False, default=None)
    Total_working_days=models.FloatField(null=True, blank=False, default=None)

       def __unicode__(self):
            return  self.Leave_type


class Leave_Balance(models.Model):                                          
        user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True,)
    Outstanding_balance=models.FloatField(null=True, blank=True,  default=None)
    Monthly_entitlement=models.FloatField(null=True, blank=True, default=None)
    Monthly_consumption=models.FloatField(null=True, blank=True, default=None)
    Leave_current_balance= models.FloatField(null=True, blank=True, default=None)
    Month=models.CharField(max_length=100,default="",null=True)
    Year=models.CharField(max_length=100,default='')

      def __unicode__(self):
         return  self.Year

The simple way to get users that have an entry in the newleave table is

User.objects.filter(newleave_set__isnull=False).distinct()

I would suggest following PEP8 for your code style as it allows programmers familiar with Python to quickly pick up the intentions of the programs that you write.

Class names should use the CapsWord convention - class NewLeave class LeaveBalance

Method names and instance variables should be lowercase words seperated by underscores NewLeave.start_date etc

The solution was quite straight forward but it took me a while to figure it out. I access the User models data via foreign key and Django actually took care of the joins.

views.py

def Allleaves(request):

allleave=NewLeave.objects.all()

return render(request,'allleave.html',locals())

and in the allleave.html, i access the the User info through the foreign key "user"

{% for leave in allleave%}

{{leave.user.first_name}}

{{leave.user.last_name}}

{{leave.Leave_type}}

{{leave.department}}

{%endfor%}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM