I have two models in Django:
class Blog(models.Model):
author = ForeignKey(Author)
post = models.CharField(max_length=100)
class Author(models.Model):
name = models.CharField(max_length=100)
I need to get the Blog entry by instance of Author:
author_one = Author (name ='John')
author_one.save()
blog_entry = Blog.objects.get(author = author_one)
Do I need to add related name to Author Foreignkey field to get results? What is the correct way get row of a table by foreign key field?
Thanks in advance.
Just to clarify a thing, using get() in this situation isn't right, quoting get() docs :
Alternatively with what Wtower mentioned,you can also use: