Reputation: 75
Hi I have two tables which are Employee(eid,eName,contact_num,e-mail,adress,salary) and Nurse(nurse_id,e_id) and the "e_id" column of Nurse model has foreign key on 'eid' of Employee model.I know how to filter by specific id, however all of them as list so that, I want to return all nurses from Employee table.You can find my models below. I am new to Django that is why any help or hint is appreciated. Thanks in advance.
class Employee(models.Model):
eid = models.IntegerField(primary_key=True)
ename = models.CharField(db_column='eName', max_length=25) # Field name made lowercase.
contact_num = models.DecimalField(max_digits=12, decimal_places=0)
e_mail = models.CharField(max_length=30)
adress = models.CharField(max_length=250, blank=True, null=True)
salary = models.IntegerField()
class Nurse(models.Model):
nurse_id = models.IntegerField(primary_key=True)
e = models.ForeignKey(Employee, models.DO_NOTHING)
Upvotes: 1
Views: 55
Reputation: 3392
from an instance of employee class you can get the nurses as follows
employee_1 = Employee.objects.first()
nurses = employee_1.nurse_set.all()
can call in templates of list view of employee by
{% for obj in object_list %}
{{ obj.ename }}
{% for nurse in obj.nurse_set.all %}
{{ nurse.eid }}
{% endfor %}{% endfor %}
Upvotes: 2