save() method could return the object

78 views
Skip to first unread message

Chris Wilson

unread,
Oct 12, 2012, 9:25:15 AM10/12/12
to Django Developers
Hi all,

If the save() method returned the object itself, then we could chain it
like this:

old_status = Status(last_contact=None).save()

Instead of having to do this:

old_status = Status(last_contact=None)
old_status.save()

It's a trivial one-line change to the Model class and I don't think it
would break backwards compatibility, as I doubt that anyone is relying on
the save() method returning None. Should I submit a patch?

Cheers, Chris.
--
Aptivate | http://www.aptivate.org | Phone: +44 1223 967 838
Future Business, Cam City FC, Milton Rd, Cambridge, CB4 1UY, UK

Aptivate is a not-for-profit company registered in England and Wales
with company number 04980791.

David Winterbottom

unread,
Oct 12, 2012, 9:33:01 AM10/12/12
to django-d...@googlegroups.com
While such a change is initially appealing, it violates the command-query separation principle in that a 'command' method such as 'save' should not return anything. 

Hence, it's not a good idea to make this change.  It's more important to have clearly purposed methods than saving a line of code.

David


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to django-developers+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.





--
David Winterbottom
Head of Programming

Tangent Labs
84-86 Great Portland Street
London W1W 7NR
England, UK


Marijonas Petrauskas

unread,
Oct 12, 2012, 9:34:48 AM10/12/12
to django-d...@googlegroups.com
There already exists create method that does exactly what you need:
obj = SomeModel.objects.create(name='foo', age=42)

To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.

Yo-Yo Ma

unread,
Oct 12, 2012, 9:36:19 AM10/12/12
to django-d...@googlegroups.com
+1

A lot of people are overriding ``save`` and not returning anything, but this isn't going to hurt them (ideally, they should already be returning the result of ``super(``, but nobody does).

Alex Gaynor

unread,
Oct 12, 2012, 9:37:43 AM10/12/12
to django-d...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/WkAiV5BsEiEJ.

To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

This isn't going to happen. It violates a fundamental API design in Python: methods which mutate/have side effects return none, methods which return a new object... return a new object. This has a side effect, and doesn't construct a new object, ergo, it returns None.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero

Chris Wilson

unread,
Oct 12, 2012, 9:40:01 AM10/12/12
to django-d...@googlegroups.com
On Fri, 12 Oct 2012, Marijonas Petrauskas wrote:

> There already exists create method that does exactly what you need:obj = SomeModel.objects.create(name='foo', age=42)

OK, thanks, that appears to be completely undocumented.

Alex Gaynor

unread,
Oct 12, 2012, 9:40:56 AM10/12/12
to django-d...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to django-developers+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.


https://docs.djangoproject.com/en/1.4/ref/models/querysets/#create
Reply all
Reply to author
Forward
0 new messages