Pulling data from two table

12 views
Skip to first unread message

sum abiut

unread,
Jan 8, 2015, 4:16:25 PM1/8/15
to django...@googlegroups.com
Hi,
can someone please help. i am trying to pull data from two table in django and display the results. can someone please point me to the right direction.

kind regards,
Sum A


Vijay Khemlani

unread,
Jan 8, 2015, 4:33:20 PM1/8/15
to django...@googlegroups.com
Please be more specific in your question, how are these tables related? what models are they associated with?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y6DxaBuSiLYNr2dc3sAn70qB46_G5MTJzPoJPkG1-57PA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

sum abiut

unread,
Jan 8, 2015, 4:50:55 PM1/8/15
to django...@googlegroups.com
Hi Vijay,
Thank you for your email. Here is my models. i want to be able to pull out all the data from both table and display them. i am a bit new to django still finding my way out.


class staff(models.Model):
    First_Name = models.CharField(max_length=45)
    Last_Name =models.CharField(max_length=45)
    employee_Date=models.DateField()
    position=models.CharField(max_length =45)
    department=models.CharField(max_length =45)
    qualification=models.CharField(max_length =45)
    VNPF_no =models.CharField(max_length =45)
    salary =models.IntegerField()
    montly_salary=models.IntegerField()
    Training=models.TextField()
    username  =models.ForeignKey(User, default =1)
   
    def __unicode__(self):
        return self.First_Name
       
class leave(models.Model):
    leave_type =models.CharField(max_length=45)
    Specify_details=models.TextField(default="")
    start_Date =models.DateField()
    end_date=models.DateField()
    total_working_days=models.IntegerField()
    authorization =models.CharField(max_length=45)
    authorized_by=models.CharField(max_length=45,  default ="")
    remarks=models.TextField()
    authorizaion_date =models.DateField()
    username  =models.ForeignKey(User,  default =1)
    staff =models.ForeignKey(staff,  default =1)
    
    def __unicode__(self):
        return self.leave_type






For more options, visit https://groups.google.com/d/optout.



-

Vijay Khemlani

unread,
Jan 8, 2015, 8:08:27 PM1/8/15
to django...@googlegroups.com
OK, what query are you trying to make? or what information do you want to display?

sum abiut

unread,
Jan 8, 2015, 9:42:03 PM1/8/15
to django...@googlegroups.com
Hi,
i am trying to pull out all the information in both model. i can pull out information in each table but i am a but confuse in joining the two tables together. something like NATURAL JOIN in mysql.

Cheers,



For more options, visit https://groups.google.com/d/optout.



-

Vijay Khemlani

unread,
Jan 8, 2015, 11:15:48 PM1/8/15
to django...@googlegroups.com
Well, you can do something like this

one_leave = leave.objects.get(pk=1) # Asuming there is a leave with pk 1
print one_leave.staff.First_Name    # Automatically joins with the staff table to print the first name of the associated staff

Or something like this

one_staff = staff.objects.get(pk=1) # Asuming there is a staff with pk 1
staff_leaves = one_staff.leave_set.all()
for staff_leave in staff_leaves:
    print staff leaves                    # Print all the leaves of a particular staff

Also, I recommend you to follow Python and Django naming conventions, that is classes in CamelCase ("Staff" and "Leave" instead of "staff" and "leave") and fields in underscore (first_name instead of First_Name, etc)

sum abiut

unread,
Jan 8, 2015, 11:23:04 PM1/8/15
to django...@googlegroups.com
Hi Vijay,

Thanks very much for your assistance and recommendation. i will get back to you if needed more help.

Sum

Reply all
Reply to author
Forward
0 new messages