3

I suppose this must be possible judging by this post but I can't seem to work out the syntax. I need to get the blog object with the picture field via the author foreign key.

Is this possible with get_object_or_404, and if so how?

#models.py
class Blog(models.Model):
    author = models.ForeignKey(MyUser)

#author
class MyUser(AbstractUser):
    picture = models.ImageField()

blog = get_object_or_404(Blog, pk=blog_id)
Community
  • 1
  • 1
rix
  • 10,104
  • 14
  • 65
  • 92
  • 1
    It is not clear what you're asking. Sounds like you mean `get_object_or_404(Blog, author__picture='somefilename.jpg')`, but that doesn't make much sense. – lanzz Mar 12 '14 at 17:20
  • Your blog object will have access to the author and profile. blog.author.picture should yield your result though it will cause a query. – Victor 'Chris' Cabral Mar 12 '14 at 17:24
  • ok right, i understand. so in 1 one query i just want to get the blog by id, and also the author picture. I guess that's not possible with get_object_or_404? – rix Mar 12 '14 at 17:28
  • 2
    It does seem possible. http://stackoverflow.com/questions/6624530/django-using-select-related-and-get-object-or-404-together – Victor 'Chris' Cabral Mar 12 '14 at 17:28
  • Yep, it does seem so, but i'm struggling to figure out how without a concrete example.. – rix Mar 12 '14 at 17:32
  • You don't have to fetch them both. Get Blog object first and then use blog.author.picture whenever you need to use The picture. – Selcuk Mar 12 '14 at 19:48
  • 1
    That's 2 database hits though isn't it? – rix Mar 12 '14 at 22:11

2 Answers2

1

The query you want is like so (from the docs):

from django.db import models

class City(models.Model):
    # ...
    pass

class Person(models.Model):
    # ...
    hometown = models.ForeignKey(City)

class Book(models.Model):
    # ...
    author = models.ForeignKey(Person)

And then your query is:

b = Book.objects.select_related('author__hometown').get(id=4)
p = b.author         # Doesn't hit the database.
c = p.hometown       # Doesn't hit the database.

b = Book.objects.get(id=4) # No select_related() in this example.
p = b.author         # Hits the database.
c = p.hometown       # Hits the database.

except in your case your query would be: Picture.objects.select_related('author__blog').get(picture_name='somefilename.jpg')

You're saying I want the picture (and while your at it, the Author and their Blog) that relates to the picture name 'somefilename.jpg'. At least if I've understood your question and DB structure it is.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • Well this was certainly a long time ago and whilst i appreciate it wasn't explained very well, it seems that @victor-chris-cabral had the correct reponse which is here - http://stackoverflow.com/questions/6624530/django-using-select-related-and-get-object-or-404-together – rix Jan 20 '15 at 15:17
  • @rix absolutely, but an answer is always better than a comment (comments disappear more easily). Also this should constitute a concrete example, for anyone else coming here. – AncientSwordRage Jan 20 '15 at 16:57
0

As Pureferret suggested in his answer you can do it in following way,

blog = Blog.objects.get(pk=blog_id)
author = blog.author

and if you want to show that image in template just use

{{ auther.picture.url }}
shellbot97
  • 198
  • 1
  • 12