forms of 2 tables upload files

25 views
Skip to first unread message

dk

unread,
Mar 17, 2015, 10:55:57 AM3/17/15
to django...@googlegroups.com
i have 2 tables,   1 table contain the fields for name, email,
and I did another table with file, and date.

and I am trying to populate the 2 tables when a user submit the form.

I follow this amazing tutorial
but he is using the trick of using the models to do the form.   witch you can only use one table for that.

in the mean time we change the code and we are using just 1 table, with name email, file and date and then doing it exactly like the tutorial,
and my view is like this
def request_page(request):
if request.method == "POST":
form = Submit_Form(request.POST, request.FILES)
if form.is_valid():
email = form.cleaned_data["email"]
per = Person(email=email, date=datetime.datetime.now(), file=request.FILES['file'])
per.save()

message = "Thanks, Submission accepted: " + email
forma = Submit_Form()
# return redirect("request_page", {"form": forma, "message": message})
return render(request, "submit_request.html", {"form": forma, "message": message})
else:
form = Submit_Form()

return render(request, "submit_request.html", {"form": form, "message": ""})




I can make my own form  like this

# class NameForm(forms.Form):
# name = forms.CharField(label='Name', max_length=100, required=True, initial="")
# last_name = forms.CharField(label='Last Name', max_length=100, required=True, initial="")
# email = forms.EmailField(label="Email", required=True, initial="")

#     the_file = forms.FileField(required=True)
#     date = forms.DateTimeField()


will render properly but later on I don't know how to populate the 2 tables.   
any one have a little example that I can follow to .save() the person table and the file table?

thank you guys. =)

Filipe Ximenes

unread,
Mar 17, 2015, 1:00:46 PM3/17/15
to django...@googlegroups.com
You can keep the ModelForm just like the tutorial and override the "save" method.
It will look something like this:

class MyForm(forms.ModelForm):
the_file = forms.FileField(required=True)

class Meta:
model = Person
fields = ('name', 'email', 'the_file')

def save(self, commit=True):
person = super(MyForm, self).save(commit)

file_instance = FileModel(the_file=self.cleaned_data['the_file'])

if commit:
file_instance.save()

return person
--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br
Reply all
Reply to author
Forward
0 new messages