Reputation: 525
I am newbie in Django and notably its inheritance tools. I have the following architecture:
class BaseMixin(models.Model):
effective_from = models.DateTimeField(blank = True, null = True)
class Meta:
abstract = True
class QuerySetManager(models.Manager):
def get_queryset(self):
# ...
def __getattr__(self, attr, *args):
return getattr(self.get_queryset(), attr, *args)
def save(self, *args, **kwargs):
# I want this method to be deployed for my_model_instance.save()
class MyModel(BaseMixin):
# ...
objects = QuerySetManager()
class Meta:
managed = False
db_table = 'my_model'
class QuerySet(QuerySet):
# ...
So my goal is to call QuerySetManager
's save
method when trying to save changes to my_model_instance. The question is, should I overload save
method in BaseMixin
? Or in QuerySetManager
? I tied both, and as for now, Django ignores my custom save method in both cases.
Upvotes: 0
Views: 683
Reputation: 4643
Your BaseMixin is abstract class which you are inheriting at other places.
ModelManagers doesn't provide a save method , it's meant for abstracting out complex logic while querying a particular model.
You need to override save MyModel here. Something like.
class MyModel(BaseMixin):
# ...
objects = QuerySetManager()
class Meta:
managed = False
db_table = 'my_model'
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
print "In Save Method"
return super(MyModel, self).save()
Save method is property of model instance.
Simple Example.
def my_view(request,pk):
my_model = MyModel.object.get(id=pk)
my_model.field_1 = "new value"
## When you call save method your custom save method is called first.
my_model.save()
Upvotes: 2