Django admin - how to save inline strings?

I need to override the inline save method in admin. When saving photos, I need to add the user ID in the DB column. I cannot do this in the model because there is no query data there. How can I do this in admin to somehow set the user id?

+4
source share
2 answers

I believe the save_formset method for ModelAdmin is what you should use:

class ArticleAdmin(admin.ModelAdmin): def save_formset(self, request, form, formset, change): instances = formset.save(commit=False) for instance in instances: instance.user = request.user instance.save() formset.save_m2m() 

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset

+11
source

I am relatively new to django (1.8) and using the above:

  def save_formset(self, request, form, formset, change): instances = formset.save(commit=False) # gets instance from memory and add to it before saving it for obj in formset.deleted_objects: obj.delete() for instance in instances: for form in formset: # cleaned_data is only available on the form, so you have to iterate over formset instance.modified_by = request.user instance.created_by = request.user instance.lowercase_enum_value_en = form.cleaned_data['enum_value_en'].lower() instance.save() formset.save_m2m() 

i.e. adding to it before saving the instance and form, however, when the user enters 2 lines, he always saves the last cleaned_data ['enum_value_en'].

-1
source

All Articles