Filling out a form using a json file

25 views
Skip to first unread message

joche...@gmail.com

unread,
Dec 12, 2016, 9:17:11 AM12/12/16
to django...@googlegroups.com
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

Abraham Varricatt

unread,
Dec 12, 2016, 1:15:13 PM12/12/16
to Django users
Hello Jochen,


On Monday, December 12, 2016 at 9:17:11 AM UTC-5, joche...@gmail.com wrote:

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?

What about using the forms validate() method? i.e. if the user has not filled in all the JSON details, return a validation error. This should give them the opportunity to make edits or re-type as needed.

 
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.

This sounds like a validation job to me. Just put in all the logic checks into the validate() method. Bear in mind that you are likely to receive bad input, so handle accordingly. 

Yours sincerely,
Abraham V.

joche...@gmail.com

unread,
Dec 13, 2016, 7:38:12 AM12/13/16
to 'Abraham Varricatt' via Django users
Hello Abraham,

thanks for your answer!

On Mon, 12. Dec 10:15, 'Abraham Varricatt' via Django users <django...@googlegroups.com> wrote:
> What about using the forms validate() method? i.e. if the user has not
> filled in all the JSON details, return a validation error. This should give
> them the opportunity to make edits or re-type as needed.

Yes, I probably should validate the input that way, but what I'm
concerned with at the moment is how the JSON details make it to the
form object in the first place.
I probably got overwhelmed reading the API Docs and thought there has
to be a more elegant way to handle the JSON -> Form Object part than
just writing a ``post`` method.

> > 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.
> >
>
> This sounds like a validation job to me. Just put in all the logic checks
> into the validate() method. Bear in mind that you are likely to receive bad
> input, so handle accordingly.

As in validating the ModelForms for each object and choose the one
that validates successfully if any?

Jochen
Reply all
Reply to author
Forward
0 new messages