Reputation: 35
I was going through the Django's official Polls Tutorial. I am able to follow the steps and so far able to get the results. But I am not able to understand the structure of Models
While using the API in shell (Polls Tutorial Part 1) one use Polls.objects.all() to list objects Now polls is a class which inherits from models.Model. But what about objects?
Using Poll.objects lists objects to and then we can use the all method on it.
So what exactly is Poll.objects (an instance of something else?) Sorry if this sounds really dumb I am very new to all this stuff.
Upvotes: 1
Views: 500
Reputation: 474001
Poll.objects
is a special thing in Django called Manager
:
A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.
The name objects
is just a convention/standard that Django follows. You can easily change it:
from django.db import models
class MyModel(models.Model):
whatever = models.Manager()
You can also define your custom model manager methods. Often it is really a good way to extract and reuse functionality related to database model interaction. For example:
Upvotes: 3