I have the following models in my django app.
class author(models.Model):
name=models.CharField(max_length=100)
class book(models.Model):
name=models.TextField()
authors=models.ManyToManyField(author)
A author definately have written multiple books. A single book can also have multiple authors.
I want to iterate through all books by a particular author. Till now I have tried
for b in book.objects.filter(authors__name='HiDe'):
print(b)
which gives AttributeError: 'function' object has no attribute 'objects'
probably because authors in book is a ManyToManyField.
Adding books=models.ManyToManyField(author)
will make the database large and cumbersome to manage as an author can have too many books, so this is not the solution I want.
CodePudding user response:
How do you define the book
?
It seems it is just a function. Not a model object.
The error tells you books
doesn't have the attribute objects
, and is a function.
It is also generally good practice to capitalise the first letter of Django models.