On Thu, Mar 25, 2010 at 3:47 PM, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > On Thu, 2010-03-25 at 11:22 +0530, Kenneth Gonsalves wrote: >> hi, >> >> I have a model like this: >> >> name - unique >> slno - not null >> mode - not null >> >> If the instance does not exist and I give all three values to get_or_create, >> it works >> >> if the instance exists, and I give values for slno and mode which are >> different >> from the existing ones - I get a programming error as get_or_create tries to >> create a new instance and falls foul of the unique constraint on name. It >> works if I only give the value for name - it gets the instance and then I can >> add the new values and save > > 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). > > Malcolm > >
He is describing this situation: >>> User.objects.get(username='tevans...@googlemail.com') <User: tevans...@googlemail.com> >>> User.objects.get_or_create(username='tevans...@googlemail.com') (<User: tevans...@googlemail.com>, False) >>> User.objects.get(username='tevans...@googlemail.com').first_name u'Tom' >>> User.objects.get(username='tevans...@googlemail.com', first_name='Thomas') Traceback (most recent call last): File "<console>", line 1, in <module> File "....site-packages/django/db/models/manager.py", line 119, in get return self.get_query_set().get(*args, **kwargs) File "....django/db/models/query.py", line 305, in get % self.model._meta.object_name) DoesNotExist: User matching query does not exist. >>> User.objects.get_or_create(username='tevans...@googlemail.com', >>> first_name='Thomas') Traceback (most recent call last): File "<console>", line 1, in <module> File "....site-packages/django/db/models/manager.py", line 122, in get_or_create return self.get_query_set().get_or_create(**kwargs) File "....site-packages/django/db/models/query.py", line 343, in get_or_create raise e IntegrityError: (1062, "Duplicate entry '' for key 2") IE, where Model.objects.get(**kwargs) does not return an object, but Model.objects.create(**kwargs) would fail, which is fixed by knowing about the 'defaults' kwarg to get_or_create: >>> User.objects.get_or_create(username='tevans...@googlemail.com', ... defaults={'first_name': 'Thomas', }) (<User: tevans...@googlemail.com>, False) Cheers Tom -- 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.