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


-- 
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.

Reply via email to