tickets_full: 6406

This data as json

id created changetime last_pulled_from_trac stage status component type severity version resolution summary description owner reporter keywords easy has_patch needs_better_patch needs_tests needs_docs ui_ux
6406 2008-01-17 20:10:25 2011-09-28 16:12:16 2019-06-24 01:23:51.393170 Accepted closed contrib.admin     newforms-admin duplicate form_save_add and form_save_change method for ModelAdmin ModelAdmin calls form.save() inside the save_add method: {{{ def save_add(self, request, model, form, formsets, post_url_continue): ... new_object = form.save(commit=True) if formsets: for formset in formsets: formset.instance = new_object formset.save() pk_value = new_object._get_pk_val() ... }}} I want to be able to hack the form.save method and calculate aditional data to the object based on the request. Basically, I want to attach the request.user to an attribute of my just added object. I have a solution, I can call a save_form_add method of ModelAdmin that saves the form: {{{ def save_add(self, request, model, form, formsets, post_url_continue): ... new_object = self.save_form_add(request, model, form, formsets, post_url_continue) pk_value = new_object._get_pk_val() ... def save_form_add(self, request, model, form, formsets, post_url_continue): new_object = form.save(commit=True) if formsets: for formset in formsets: formset.instance = new_object formset.save() return new_object }}} This way I can do something like this: {{{ class ObjectOptions(admin.ModelAdmin): def form_save_add(self, request, model, form, formsets, post_url_continue): new_object = form.save(commit=False) new_object.user = request.user new_object.save() if formsets: for formset in formsets: formset.instance = new_object formset.save() return new_object }}} I didnĀ“t find any other solution to intercept the object save and have access to the request. What do you think about the addition of save_form_add and save_form_change? I will write the proper patch. Best regards! nobody michelts form object save commit   0 0 0 0  
Powered by Datasette · Query took 2.678ms