I'm currently getting into proxy models and I actually cannot understand when we should give them respect. For me they look very similar to managers
Is there some differences or we can implement the same things using proxy models and managers?
Django managers are classes that manage the database query operations on a particular model. Django model manager
Proxy models allow you to create a new model that inherits from an existing model but does not create a new database table. proxy model
Let me give you an example:
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
published_date = models.DateField()
is_downloadable = models.BooleanField(default=True)
If this is your model
manager:
class BookMnanger(models.Manager):
def by_link(self):
return self.filter(is_downloadable=True)
and you new manager:
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
published_date = models.DateField()
is_downloadable = models.BooleanField(default=True)
# your new manager should register to your model
downloading = BookMnanger()
now, your new custom manager can work as below:
my_books = Book.downloading.all()
print(my_books)
but the proxy:
class BookProxy(Book):
class Meta:
proxy = True
def special_method(self):
return f"{self.title} by {self.author}, published on {self.published_date}"
and your proxy can work like this:
book = BookProxy.objects.first()
print(book.special_method())
proxies are the way to change behavior of your model but, managers will change your specific queries
I can give you more link about them, if you need?