Sobes.tech
Junior — Middle

How can you access the model manager in Django?

sobes.tech AI

Answer from AI

In Django, access to the model manager is provided through the objects attribute of the model class by default. For example, if you have a Book model, you can get all objects like this:

books = Book.objects.all()

You can also define a custom manager by creating a class that inherits from models.Manager and assigning it to the model:

class CustomManager(models.Manager):
    def active(self):
        return self.filter(is_active=True)

class Book(models.Model):
    title = models.CharField(max_length=100)
    is_active = models.BooleanField(default=True)

    objects = CustomManager()

# Usage
active_books = Book.objects.active()

Thus, a manager is an interface for database queries accessible through the model attribute.

How can you access the model manager in Django… - sobes.tech