On Thursday 25 Mar 2010 9:17:12 pm Malcolm Tredinnick wrote: > I don't understand what you are saying here. If the instance exists (as > in, the filter parameters passed to get would return a single item), > that instance is returned and the default parameters are never > considered. So the values you pass for slno and mode are irrelevant. If > the equivalent get() call would not return anything, then the instance > does not exist and it's clear that the default parameter must contain > correct data that can be used to create a new object -- including not > violating any unique constraints. Perhaps you could show the code call > you are making, rather than writing it out as a sentence. > > In short: get_or_create() first runs the get(). If that returns > something, it is what is returned. If the get() fails with no object > being present, then the instance DOES NOT exist and a new one is > created, with all the same requirements as if you'd created a new object > from scratch (e.g. no unique constraint violations). >
sorry for not replying to this - for some reason all your recent mails have gone to the spam folder! I only became aware of this mail when I saw a reply to it. Here is what I mean - A simple model with two fields, 'name' is unique, and 'num' is not null: >>> Tm.objects.get_or_create(name='me',num=1) >>> (<Tm: me>, True) >>> Tm.objects.get_or_create(name='me',num=2) >>> Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.6/django/db/models/manager.py", line 135, in get_or_create return self.get_query_set().get_or_create(**kwargs) File "/usr/lib/python2.6/django/db/models/query.py", line 375, in get_or_create transaction.savepoint_rollback(sid, using=self.db) File "/usr/lib/python2.6/django/db/transaction.py", line 242, in savepoint_rollback connection._savepoint_rollback(sid) File "/usr/lib/python2.6/django/db/backends/__init__.py", line 61, in _savepoint_rollback self.cursor().execute(self.ops.savepoint_rollback_sql(sid)) File "/usr/lib/python2.6/django/db/backends/__init__.py", line 75, in cursor cursor = self._cursor() File "/usr/lib/python2.6/django/db/backends/postgresql/base.py", line 149, in _cursor cursor.execute("SET client_encoding to 'UNICODE'") ProgrammingError: ERROR: current transaction is aborted, commands ignored until end of transaction block SET client_encoding to 'UNICODE' -- regards Kenneth Gonsalves Senior Associate NRC-FOSS http://certificate.nrcfoss.au-kbc.org.in -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.