Yesterday I was wiring up a toggle method. If the row is in the
database, remove it. If it isn't in the database, add it.
I don't have the exact code in front of me but it was something like
this:
def mytoggle(var):
try:
setting = Setting.objects.get(var=var)
setting.delete()
except DoesNotExist:
setting.objects.create_setting(var=var).save()
except AssertionError:
setting.objects.filter(var=var).delete()
The check for AssertionError is a "just in case" check that somehow
the variable slipped in there twice. But I'm wary of attaching code
to an assertion error.
I don't have a lot of Python experience so maybe that's reasonable.
Thanks,
Rob
+1
Ticket and patch created:
http://code.djangoproject.com/ticket/3511
There are some notes on the wiki about how the model system works,
useful for anyone who wants to venture in there and patch stuff:
http://code.djangoproject.com/wiki/DevModelCreation
Most things on the model class itself (including its default
exceptions) are added during the call to __new__ (including all the
default model methods, which are curried and attached).
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."