I am new to django and I have an Survey app in which the admin creates surveys which have questions, the questions have choices... I have added the save_as = True to my survey admin, however when I copy a survey, the questions are present in the copy, but not the choices..
class SurveyAdmin(admin.ModelAdmin):
save_as = True
prepopulated_fields = { "slug": ("name",),}
fields = ['name', 'insertion', 'pub_date', 'description', 'external_survey_url', 'minutes_allowed', 'slug']
inlines = [QuestionInline, SurveyImageInLine]
I have attempted to make use of deepcopy in the save_model method, but get "NOT NULL constraint failed: assessment_question.survey_id", from the traceback, it appears that the pk of the question is None when attempting to save. Is there a better way to copy the surveys through the admin or how can I fix my application of deepcopy?
def save_model(self, request, obj, form, change):
if '_saveasnew' in request.POST:
new_obj = deepcopy(obj)
new_obj.pk = None
new_obj.save()
Thank you for your help in advance.
Ended up ditching save_as all together and wrote an admin action that properly copies all the fields I need...