0

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.

1 Answer 1

2

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() 
Sign up to request clarification or add additional context in comments.

4 Comments

Bipul, didn't understand your response in all honesty. And what is the syntax of calling the save method with such approach ?
Is def save just a simple view ?
Sorry def save was indentation issue :) added more example.
Simply it's a bit repetitive. I mean, I have to define the same save method for all children of BaseMixin. And this method is absolutely the same. I have separated the code into a view and then I write `def save(self,...) { standard_save_method(self, ....)}. Well, I've minimized the repetition, but still it's a bit irritating that I have to add this bunch of lines for each and every non-abstract class throughout the entire project

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.