Hi,
I have a json file describing an object that I want to import into my
app. For that purpose, I want to upload it using a form field and fill
a ModelForm so the contents can be checked and edited by a user before
saving it to the database. This is what I'm currently doing:
# in forms.py
class MyObjectRequestForm(forms.ModelForm):
"""
Form for generating a MyObject from a json file
"""
class Meta:
model = MyObject
exclude = ()
# in views.py
class MyObjectFromFile(LoginRequiredMixin, View):
def post(self, request):
"""
generate Form from json in 'message_file' parameter.
"""
context = dict()
received_object = json.load(request.FILES['message_file'])
new_my_object = MyObject()
new_my_object.derive_from_message(received_object)
context['form'] = MyObjectRequestForm(model_to_dict(new_my_object))
return render(request, 'myapp/my_object_form.html', context)
I already have a CreateView and UpdateView for this kind of object so
I probably should inherit from one of those to take advantage of the
``get_success_url`` method and to avoid writing a completely new view
for editing Objects, but it seems that those views can only deal with
objects that already are in the database. So is there a more
idiomatic way to solve this or am I stuck with the above solution?
On another note, there are several Models I want to work with in
this way so it would be even better if I could select the model
class according to the contents of the json file.
Kind regards,
Jochen
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/20161212125441.GC26289%40gmail.com.
For more options, visit https://groups.google.com/d/optout.