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.schinc...@gmail.com> On Dec 15, 4:13 am, Continuation <selforgani...@gmail.com> wrote: > 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. 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 -- 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.
signature.asc
Description: OpenPGP digital signature