Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django metaview experiment
def edit_model(cls, get_dict, update_keys, update_dict_eval):
def _edit_model_view(request):
instance = get_object_or_404(cls, **get_dict)
model_name = cls_to_name(cls)
ModelForm = eval(model_name + 'Form')
if request.method == 'POST':
form = ModelForm(request.POST, request.FILES)
if form.is_valid():
updated_instance = cls()
for key in update_keys:
try:
updated_instance.update(**{
key: form.cleaned_data.get(key) or eval(key)
})
except:
continue
updated_instance.update(**{k: eval(v) for k, v in update_dict_eval})
updated_instance.save()
else:
form = ModelForm(instance=instance)
return render(request, "edit_" + model_name + ".html", locals())
return _edit_model_view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.