Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing an argument to a custom save() method

How do I pass an argument to my custom save method, preserving proper *args, **kwargs for passing to te super method? I was trying something like:

form.save(my_value)

and

def save(self, my_value=None, *args, **kwargs):

   super(MyModel, self).save(*args, **kwargs)

   print my_value

But this doesn't seem to work. What am I doing wrong?

Edit: I found this example (see the last message, for passing 'reorder'): http://groups.google.com/group/django-users/browse_thread/thread/b285698ea3cabfc9/6ce8a4517875cb40?lnk=raot

This is essentially what I am trying to do, but my_value is said to be an unexpected argument for some reason.

like image 923
Nikita Avatar asked Mar 29 '10 02:03

Nikita


3 Answers

The existing answers are incomplete.

The problem with Model.save is that there are multiple ways to get to it, most notably through Model.objects.create(). A queryset create() call will not pass additional arguments to Model.save and your code might not behave as expected.

You can easily see the source code for create() and other methods (get_or_create, update_or_create) here: https://github.com/django/django/blob/2.2.7/django/db/models/query.py#L415

The bulk_create method of QuerySet does not call save at all. Which means any custom logic will be entirely ignored.

Because of these risks I would consider the premise of the question an anti-pattern to be avoided. Rather than overloading the built in save method, create an entirely different (factory) method to encompass your special behavior e.g. mymodel.special_save().

like image 133
mastaBlasta Avatar answered Oct 25 '22 11:10

mastaBlasta


Keyword arguments must follow the positional arguments. Try this instead:

def save(self, my_value, *args, **kwargs):
    ....

or:

def save(self, *args, **kwargs):
    my_value = kwargs.pop('my_value', None)
like image 37
Daniel Naab Avatar answered Oct 25 '22 11:10

Daniel Naab


You can try the following.

Override the save method as:

def save(self, my_val, *args, **kwargs):
    print my_val
    # do_something here
    return super(MyModel, self).save(*args, **kwargs)

On calling the save method as:

MyModel.save(my_val="fooooo")

this will print my_val and save it.

like image 42
ashish singh Avatar answered Oct 25 '22 09:10

ashish singh