model save question

7 views
Skip to first unread message

Mike Dewhirst

unread,
Aug 16, 2011, 3:21:24 AM8/16/11
to django...@googlegroups.com
When using save() in a model, what is the difference between ...

save(self, force_insert=False, force_update=False) or
save(self, force_insert=False, force_update=False, **kwargs)
# whatever
super(Xyz, self).save(force_insert, force_update) or
super(Xyz, self).save(force_insert, force_update, **kwargs)

and

save(self, *arg, **kwargs)
# whatever
super(Xyz, self).save(*args, **kwargs)

I see the former in a fair bit of django example code around the place
and the latter in the docs here

https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods

Thanks for any insights

Mike

Daniel Roseman

unread,
Aug 16, 2011, 4:10:19 AM8/16/11
to django...@googlegroups.com
There's no practical difference right now. The main reason for the (*args, **kwargs) format is for forwards compatibility: if Django introduces a new argument to `save` in future versions, your code might stop working if you've hard-coded the function to only pass two parameters.
--
DR.

Mike Dewhirst

unread,
Aug 16, 2011, 4:22:03 AM8/16/11
to django...@googlegroups.com
Thanks DR

Mike

> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/zk3ttVQDbSgJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Yaşar Arabacı

unread,
Aug 16, 2011, 3:23:20 AM8/16/11
to django...@googlegroups.com
Absence of **kwargs would cause problems if for example save method is called with "using" keyword argument. You could either list all the available keyword arguments or use **kwargs.

By the way, is there any other kwarg for save?

2011/8/16 Mike Dewhirst <mi...@dewhirst.com.au>
--
You received this message because you are subscribed to the Google Groups "Django users" group.

Mike Dewhirst

unread,
Aug 16, 2011, 11:27:51 PM8/16/11
to django...@googlegroups.com
Yaşar

Thanks .. the "using" kw hadn't occurred to me but I don't know of any
others. I'm happy now. I figure the force_* kwargs were probably the
only ones then and it wasn't too difficult for example code to mention
them. I prefer the *args, **kwargs version anyway. It hides the gory
detail from me.

Cheers

Mike

On 16/08/2011 5:23pm, Yaþar Arabacý wrote:
> Absence of **kwargs would cause problems if for example save method is
> called with "using" keyword argument. You could either list all the
> available keyword arguments or use **kwargs.
>
> By the way, is there any other kwarg for save?
>
> 2011/8/16 Mike Dewhirst <mi...@dewhirst.com.au

> <mailto:mi...@dewhirst.com.au>>


>
> When using save() in a model, what is the difference between ...
>
> save(self, force_insert=False, force_update=False) or
> save(self, force_insert=False, force_update=False, **kwargs)
> # whatever
> super(Xyz, self).save(force_insert, force_update) or
> super(Xyz, self).save(force_insert, force_update, **kwargs)
>
> and
>
> save(self, *arg, **kwargs)
> # whatever
> super(Xyz, self).save(*args, **kwargs)
>
> I see the former in a fair bit of django example code around the
> place and the latter in the docs here
>
> https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods
>
> Thanks for any insights
>
> Mike
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com

> <mailto:django...@googlegroups.com>.


> To unsubscribe from this group, send email to
> django-users...@googlegroups.com

> <mailto:django-users%2Bunsu...@googlegroups.com>.

Reply all
Reply to author
Forward
0 new messages