On Tue, Jan 5, 2010 at 8:09 AM, greatlemer <greatle...@googlemail.com>wrote:
> On Jan 5, 4:00 am, Marc Aymerich <glicer...@gmail.com> wrote: > > Hi! > > I have a model like this: > > > > class company(models.model): > > name = models.CharField(mx_length=20) > > class employee(models.model): > > comany = models.ForeignKey(company) > > class user(models.model): > > person = models.OneToOneField(person) > > > > One company can have multiple employees but one employee only can have > one > > user. > > > > I know how to get all employees for a company > > > > company.objects.get(name='companyname').employee_set.all() > > > > But.. How can I get all users for a company? > > > > I try with > > > > > user.objects.get(employee=company.objects.get(name='companyname').employee_ > set.all()) > > company.objects.get(name='companyname').employee_set.all().user > > > > but this approach only works with a single object, like this: > > > > > user.objects.get(employee=company.objects.get(name='companyname').employee_ > set.all()[2]) > > company.objects.get(name='companyname').employee_set.all()[2].user > > > > any idea in how can I get all users for a company? > > > > Thanks a lot! > > Marc. > > You want to use filter [1] with something like: > user.objects.filter(employee__company__name='companyname') > > > [1] > http://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters > -- > > G > Thank you very much!! :D > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.