Re: Filling out a form using a json file

2016-12-13 Thread jochen . luig
Hello Abraham,

thanks for your answer!

On Mon, 12. Dec 10:15, 'Abraham Varricatt' via Django users 
 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

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/20161213081852.GA12521%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Filling out a form using a json file

2016-12-12 Thread jochen . luig
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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.