Any way to make get_or_create() to create an object without saving it to database?

3,607 views
Skip to first unread message

Continuation

unread,
Dec 14, 2009, 1:13:21 PM12/14/09
to Django users
When using get_or_create(), when created=True, is there any way to
make it so that it creates an object without saving it to DB?

I want to take the newly created object, do some validation tests, and
only save it to DB if it passes all tests.

Matt Schinckel

unread,
Dec 14, 2009, 6:04:02 PM12/14/09
to Django users
You can just create an object, using:

obj = Class(**kwargs)

This will not then be persistent, and should do what you need. Note
that validation that includes ManyToMany relations will not work.

Matt

kelvan.ma...@gmail.com

unread,
Feb 3, 2010, 4:44:09 PM2/3/10
to django...@googlegroups.com
I have a similar problem. In my database is a result class.
You need an exam, a user and the points to create a result and points are without null-values.
Now I want to use Result.objects.get_or_create(exam=e,user=u) to get the result if there already is one or create a new one.
This won't work because get_or_create tries to save the object to db and fails.
I want to get_or_create a result and change the points without manual validating if it already exists.

The postcondition have to be "result object with given exam, user and points saved in database, only one result per user/exam combination"
r, c = Exam_result.objects.get_or_create( user = u, exam = e )
r.points = p
r.save()

Result(exam = e, user = u) generates a new object i think, if I save this I have two results for one person at one exam, that's not what I want.

Sorry if I'm completly wrong with this, would be nice if somebody is able to enlighten me.

-- Florian


2009/12/15 Matt Schinckel <matt.sc...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



signature.asc

Tom Evans

unread,
Feb 4, 2010, 9:19:21 AM2/4/10
to django...@googlegroups.com
On Wed, Feb 3, 2010 at 9:44 PM, <kelvan.ma...@gmail.com> wrote:
> I have a similar problem. In my database is a result class.
> You need an exam, a user and the points to create a result and points are
> without null-values.
> Now I want to use Result.objects.get_or_create(exam=e,user=u) to get the
> result if there already is one or create a new one.
> This won't work because get_or_create tries to save the object to db and
> fails.
> I want to get_or_create a result and change the points without manual
> validating if it already exists.
>
> The postcondition have to be "result object with given exam, user and points
> saved in database, only one result per user/exam combination"
> r, c = Exam_result.objects.get_or_create( user = u, exam = e )
> r.points = p
> r.save()
>
> Result(exam = e, user = u) generates a new object i think, if I save this I
> have two results for one person at one exam, that's not what I want.
>
> Sorry if I'm completly wrong with this, would be nice if somebody is able to
> enlighten me.
>
> -- Florian
>
>

Er,
r, c = Exam_result.objects.get_or_create(user=u, exam=e,
defaults={'points': p, })
if not c:
r.points = p
r.save()

See http://www.djangoproject.com/documentation/models/get_or_create/

kelvan.ma...@gmail.com

unread,
Feb 5, 2010, 12:26:27 PM2/5/10
to django...@googlegroups.com
Oh, nice.

thx

2010/2/4 Tom Evans <teva...@googlemail.com>

--
signature.asc
Reply all
Reply to author
Forward
0 new messages