5👍
As the error clearly states, you are not allowed to access a model’s manager on a model instance, you have to access it thru the model class itself.
The clean way to do so is to use type()
, ie replace:
self.objects.all()
with
type(self).objects.all()
Now model inheritance being rather rare, if you don’t have a use for it in this case, you can just hardcode the model class instead using:
Section.objects.all()
This being said, your code is not bulletproof – you can easily have race conditions with multiple processes calling Section.save()
concurrently.
Source:stackexchange.com