On Wed, Feb 3, 2010 at 9:44 PM, <kelvan.mailingl...@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/ -- 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.