Just want to run an idea by the list for a feature improvement for the
oft-used convenience functions get_or_create and update_or_create. I'll
just talk about get_or_create for now on but everything I saw going forward
applies to both.

It's a common idiom to populate a dictionary and simply pass it straight on
to the function when creating or updating objects. While get_or_create
takes a named parameter, defaults, as the content to store/update and the
normal parameters are used for both search and content. The problem is that
quite often you just have a dictionary of content you want to throw into
the model table and don't want to separate it out into search content vs.
default content. If you don't separate it out you get a big nasty where
clause on the select that tries to compare every bit of content for the
item.

For the most part this behavior is just a nuisance and probably sub-optimal
db query. However, with the new json data type it will actually cause the
code to break as you can't pass in a json structure as a search value in a
where clause. Your app will break.

My proposal, which I've written in my own code, is to have get_or_create
automatically select the most appropriate data to use for the search clause
and then to make the rest "default" data to be applied to the found or
newly created object. It first tries to see if the primary key field is
present in the data structure. If that's not found then it goes through the
model fields and looks for any field designated as unique in the data
structure. The result is a cleaner and faster SQL request and a prevention
of the above mentioned error condition. If no primary key or unique field
is present in the data structure then behavior will continue as currently
implemented.

Is this a change that would be welcome in the django project? If so I'd  be
happy to create a pull request that implements this policy and supporting
unit test coverage.

thanx,

  -- Ben

-- 
Chief Systems Architect Proteus Technologies <http://proteus-tech.com>
Chief Fan Biggest Fan Productions <http://biggestfan.net>
Personal blog where I am not your demographic
<http://notyourdemographic.com>.

This email intended solely for those who have received it. If you have
received this email by accident - well lucky you!!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHN%3D9D7-RYgHe_a0uAKpr8mMYyvfY2CB%2BbLD6rcTSOPevwqdhQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to