1

I am having trouble querying a list of items in a many to many relationship. The model I am trying to query is below. This is a model that will indicate a worker selecting an "opening" and many workers can select the same "opening". I am trying to select the list of workers that have selected an "opening" but I am not successful.

class FavOpening(models.Model):
    opening = models.OneToOneField(Openings, blank=True, default=None)
    worker = models.ManyToManyField(Worker, blank=True, default=None)

    def __unicode__(self):
        return str(self.opening)

It is very strange because I tried this below for the type of opening based on a chosen worker(id-31) and it works, the opening that the worker has selected will print.

employee = get_object_or_404(Worker, id=31)
print employee.favopening_set.all()

But when I do it the other way round to get the worker inside the manytomany using the below, it does not work. Says it does not have attribute "favopening_set"

openingobj = get_object_or_404(Openings, id=1)
print openingobj.favopening_set.all()

I also tried to do the below to get the list of workers based on the opening(id=1) but I am getting no result - showing Workers.Worker.None, which is not true because I have at least one worker the have selected this opening.(I checked in admin)

openingobj = get_object_or_404(Openings, id=1)
print openingobj.favopening.worker

Finally, I tried this below based on some research that says to use prefetch to get a set of things - What's the difference between select_related and prefetch_related in Django ORM?, but it says that the manager is not accessible via openings instances.

openingobj = get_object_or_404(Openings, id=1)
openingobj.objects.prefetch_related('favopening_set').all()
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3655574
  • 692
  • 2
  • 9
  • 27
  • 1
    I can't work out why you tried to use `favopening_set`. It's a one-to-one relationship, it's not a set - the right accessor is `favopening` which you correctly used in the following snippet. – Daniel Roseman Jan 01 '17 at 16:36
  • Hi Daniel, I am getting the opening and not the worker when I user favopening like you advised. I tried to follow favopening with an object because I need the manager to help get a list of workers who has selected this opening, but I am getting the error that "Manager isn't accessible via FavOpening instances", its strange because when I print the dir(openingobj.favopening), it shows that objects is among the list of attributes that could be used. – user3655574 Jan 02 '17 at 02:35

1 Answers1

4

Edited response to take into account feedback in the comment

So you want to get a queryset of all worker who has selected an opening. Is code below helpful?

  openingobj = get_object_or_404(Openings, id=1)
  workers_queryset = opening.favopening.worker.all()

It may be a good idea to rename "worker" to "worker", I think.

Alex Vyushkov
  • 650
  • 1
  • 6
  • 21
  • Actually I need to get a queryset, - a list of workers who has selected the opening. I tried that anyway but when I used openingobj.favopening_set.all() - it gave me an error - 'FavOpening' object has no attribute 'all'. – user3655574 Jan 02 '17 at 02:28
  • 1
    Thanks much Alex, it works now. A slight correction, it has to be workers_queryset = openingobj.favopening.worker.all() instead of workers_queryset = opening.favopening.worker.all(). I stopped at openingobj.favopening.worker because the results was showing workers.workers.None, which I am confused about and did not continue beyond. Thank you!! – user3655574 Jan 02 '17 at 07:01
  • 1
    Glad it was helpful! Sorry for the typo, indeed it should be openingobj, not opening! – Alex Vyushkov Jan 02 '17 at 19:06