python - django many to many save admin -


i have model :

class mymodel(models.model):     name = models.charfield(max_length=100)     type = models.manytomanyfield(type, blank=true) 

here admin adding mymodel.

what want if type not provided while saving want type default teacher

type teacher has not been created. if type not provided want create type , assign if type not provided

according documentation's example, can override save_model this:

@admin.register(mymodel) class mymodeladmin(admin.modeladmin):     def save_related(self, request, form, formsets, change):         if not form.cleaned_data['type']:             type, created = type.objects.get_or_create(name="teacher")             form.cleaned_data['type'] = [type]         form.save_m2m()         formset in formsets:              self.save_formset(request, form, formset, change=change) 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -