django视图getid

class host(models.Model):
    emp = models.ForeignKey(getname)

    def __unicode__(self):
        return self.topic

In views there is the code as,

    real =[]
    for emp in my_emp:
      real.append(host.objects.filter(emp=emp.id))

This above results only the values of emp,My question is that how to get the ids along with emp values.

Thanks..

#0

Just add them to the list when you are processing my_emp list, something like that:

real = []
for emp in my_emp:
    real.append((emp.id, host.objects.filter(emp=emp.id)))

Later

for emp_id, host in real:
    # do something usefull

You can also get list of all emp objects for given host object by:

emp_list = host.emp_set.all()

#1

You probably want to do this whole thing in a single query:

Host.objects.filter(emp__in=my_emp)

which will get you queryset of all Host objects for your list of emp ids.