In my admin.py i use changeform_view method for mantain some data into an add form after save Fo example one of my admin class is:
class temp_mainAdmin(admin.ModelAdmin):
form = TempMainForm
list_filter = ('t_type',)
list_display = ('descr', 't_type', 'notes', 'dt', 'active')
def save_model(self, request, obj, form, change):
obj.user = request.user
super(temp_mainAdmin, self).save_model(request, obj, form, change)
def changeform_view(self, request, obj_id, form_url, extra_context=None):
try:
l_mod = temp_main.objects.latest('id')
extra_context = {
'lmod': l_mod,
'oId': obj_id
}
return super(temp_mainAdmin, self).changeform_view(request, obj_id,form_url, extra_context=extra_context)
except Exception:
pass
admin.site.register(temp_main, temp_mainAdmin, )
The point is that if the temp_main table has at least one record all was done, but if instead the table is blank, when i try to open "add" form i receive the error
DoesNotExist at /admin/backend/temp_main/add/ temp_add matching query does not exist.
if i remove the entire changeform_view method from my class all was done except for the fact that after press "Save and add another" i don't have my fields already populated.
Was wrong in my changeform_view method?
p.s.: i use PostgreeSQL as backend db
So many thanks in advance
You could check for query in this fashion inside method:
try:
l_mod = temp_main.objects.latest('id')
except Exception:
l_mod = None