0

I have 3 models connected in this way:

class book(models.Model):
    id = models.AutoField(primary_key=True)
    publisher= models.ForeignKey(Publisher, related_name='publisher')
class reader(models.Model):
    publisher= models.ForeignKey(Publisher)
class publisher(models.Model):
    date = models.DateTimeField()

and i'm trying to get all the reader of specific book. I tried to do: Reader.objects.select_related(publisher__id='some_id') ang got err: Unable to get repr for <class 'django.db.models.query.QuerySet'> I cant change this model, there is way I can get this information with that models? thanks.

N.Bar
  • 135
  • 1
  • 1
  • 10
  • why a `reader` has only the field `published`, a reader is supposed to read a book, right? where's the link? – Lemayzeur Jun 11 '18 at 05:56
  • There is something strange with your modeling, you link a reader to a *publisher*? People do not read publishers, they read books. – Willem Van Onsem Jun 11 '18 at 05:56
  • Furthermore the `related_name`, should probably be `'books'`, since related name is the name of the *opposite* direction: from `publisher` to `book`. I would really advice to first clean up your modelng, since essentially all business logic depends on the modeling. – Willem Van Onsem Jun 11 '18 at 05:57
  • Didn't you get any errors when you run the server apart from using "select_related"? I'm quiet sure the order of classes defines whether you can use another model as foreignkey. For example, you shouldn't be able to use publisher as foreign key in reader. – almost a beginner Jun 11 '18 at 06:27
  • @almostabeginner: in fact he should be able. But the modeling is simply "*strange*". Unless the reader in this modeling is some professional proof reader that is hired by a publisher, it is strange that a reader does not have a key to a book. – Willem Van Onsem Jun 11 '18 at 07:23
  • @WillemVanOnsem From my limited experience I always received errors when incorrectly ordering the classes. https://stackoverflow.com/questions/795930/does-order-of-declaration-matter-in-models-py-django-python – almost a beginner Jun 11 '18 at 13:29
  • @almostabeginner: yes, if you pass a *reference*. But django allows to pass a string. So `'publisher'` instead of `publisher`. But regardless of that, the question is still unclear, and the OP has not added the essential details to help. – Willem Van Onsem Jun 11 '18 at 13:30

1 Answers1

0

I think you should use filter instead of select_related like so:

publishers = Publisher.objects.filter(book_id=<your_book_id>)

Reader.objects.filter(publisher__in=publishers)
Andrei Berenda
  • 1,946
  • 2
  • 13
  • 27