On 06/06/2006, at 4:02 PM, Adrian Holovaty wrote:
> > On 6/6/06, Ian Holsman <[EMAIL PROTECTED]> wrote: >> any chance of getting a insert_or_replace function as well? >> >> to do something similar to mysql 5's >> >> INSERT INTO table (a,b,c) VALUES (1,2,3) >> ON DUPLICATE KEY UPDATE c=c+1; >> >> http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html >> >> (which is the other common pattern I'm seeing in my code) > > Maybe I'm not understanding correctly, but aren't you describing what > Django already does when you call save() on an object? If the > primary-key value is already in the database, it does an UPDATE; > otherwise it does an INSERT. Or are you wanting to follow this logic > for non-primary-key fields? > It isn't exactly the same. for some of the code i write I need to update counters. for example #times a person views a page. so I need to do this SQL {{{ insert into person_page_view ( person_id, page_id, count ) values ( ?,?,1 ) on duplicate update count=count+1 }}} now what django does at the moment is something like obj = PersonPageView.objects.get_or_create( person = X, page = Y, defaults={'count': 0)}) obj.count += 1 obj.save() where I was looking for something like obj = PersonPageView.objects.inc_counters( person=X, page=Y, defaults = {'count':1 }, { f(x): x.count += 1 } ) which would generate a single SQL call. (if possible) I hope it makes a bit more sense. > Adrian > > -- > Adrian Holovaty > holovaty.com | djangoproject.com > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers -~----------~----~----~----~------~----~------~--~---
