I’m new to django.
I have read a lot of doc’s and made some test projects and start to understand the working of django a little bit.
I’ve a question, my first but probably not my last:
I have a form for a model1 (db table1). I want to process the data of that form and save the original data of the form in model1 (db table1) and the processed data in another model2 (db table2). What to do (I think) I know but …
Where must I do the processing:
- in the view.py
- in another python script
- stored procedure in the db
- something else I can / need to do
The most basic definition of a Django view is that it’s the code that receives a request and returns a response. What happens between those two events is entirely up to you.
If someone fills out an HTML form, and a button on that form submits that form to your Django app, it’s a
view that is going to get that form to process it. That view can do whatever you want to do with that submitted form.
I can process the data in the view and no problem with storing in model1 (table1) but how can I store it in the second model (table2). Can you put me in the right direction.
There’s no difference between the two cases. You’re either creating a new instance of a model or updating an existing instance of a model - it doesn’t matter what the model is, or how many different models are involved.
Thanks for the info.
It works in my testproject as a proof of concept but I need to refine it because I think it is not djangonic (is this an excisting expression!).
I’ve another problem where I’m stuck for a few days.
- I want to use a Listview.
- Iterate over the rows of the model
- Do some calculations with some fields
- put the rows and the additional calculations in the context
- display this in a template.
Point 1,3,4,5 I’ve no problems with (not yet at least) but I’ve a problem with point 2.
How do I get each row to be able to do the calculations
I tried :
urlpatterns = [ path('x/<naam1>', views.test_list, name='index2'),]
self.naam1 = get_object_or_404(<Model>, naam1=self.kwargs['naam1'])
but i do something wrong because I get no data.
Can you please help me.
Side note: Whenever you’re posting code (or template or error message) here, surround the text between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your post, please remember to do this in the future.)
Also, when you’re posting code snippets here, please copy/paste the actual code causing problems.
The appropriate method to override in a ListView to do that type of work would be the
get_context_data method. You would call
super() to get the context - that dict contains the queryset to be rendered under both the keys
object_list and the value returned by
Point 1: OK I’ll try to remember
Point 2: the same
Point 3: thanks I’ll try it out and let you know
New question: what is the best method to know what is going on? At the moment I’m using print statement.
Kinda depends upon what you’re looking to determine. Print statements are about the easiest to use, but you need to know what you’re looking for to figure out what you want to print, so it typically ends up being an iterative process.
Using a debugger is a bit more “hassle” to use (tools like VSCode do make it easier), but can give you more insight. It also allows you to single-step your way through the code to help you follow the logic as well.
OK if I want to go deep into the code I can use VSCode or PyCharm.
If I only need to know, say the contents of the context I can use print statements.
At the moment I 'm still busy learning with a testapp / testmodel / aso, trying out things I think I will need into my real project.
In any case again thanks for your help and by the way every hint is welcome.