Created
June 23, 2017 02:24
-
-
Save elimisteve/dc02bc1158f38c72238f3eb9b488efac to your computer and use it in GitHub Desktop.
Django metaview experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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