Django create several records at a time - django

I want to be able to create as many records as a user wants for a database table in a single form.
For example, there will be some inputs for the data required for a record and at the end of the line a "+" button that would make a new line of inputs appear. There should be no limit to the number of lines and when the user clicks on the single submit button, all of the records would be inserted.
Thing is : I don't know how to make a new line appear dynamically, I suppose I have to use jquery for that but I'm kind of a newbie :)
And I don't know how I can iterate through all the lines dynamically added.
If someone can point to an example or something, it'd save me a lot of hair pulling !
EDIT :
By following this blog post I managed to do that. I have one last problem which is : when I try to insert several records at one time, it keeps the last one fine, but the previous ones are considered empty. It tells me that the fields are required, I fill them up and click on save and only then it saves them allright.
Maybe I'll ask a new question for this!

Start here: https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#model-formsets
I suppose I have to use jquery for that
That can also work.
And I don't know how I can iterate through all the lines dynamically added.
You'll get all the fields of the form (all of them) in your request.POST object. If you use a formset, it will largely be handled by the form's clean() and save() methods.

Related

Mapping user spreadsheet columns to database fields

I’m not sure where to start on this project. I know how to read the contents of the excel spreadsheet, I know how to identify the header row, I know how to loop over the contents. I believe I have the UX portion worked out but I am not sure how to process the data.
I’ve googled and only found .Net solutions but I’m looking for a ColdFusion/Lucee solution.
I have a working form allowing me to map a user's spreasheet column to my database values (this is being kept simple for this post; user does not have direct access to the database).
Now that I have my data, I'm not sure how to loop over the data results. I believe there will be several loops (an outer and an inner). Then of course I also need to loop over the file contents but I think if I can get the headings mapped out,I can figure out the remaining.
Any good links, tutorials, or guides would be greatly appreciated.
Some pseudo code might be enough to get me started.
User uploads form
System reads headers and content.
User is presented form with a list of columns from their uploaded spreadsheet to match with available database fields (eg “column1” matches “customer name”.
User submits form.
Now what?
UPDATED
Here is what the data looks like AFTER the mapping has been done in my form. The column deliiter is the ::: and within the column the ||| indicates the ID associated with the selected column value. I've included the id and the column value since I plan on displaying the mapping again as a confirmation. Having the ID saves a trip to the database.
If I understand correctly, your question is: how do you provide the user a form allowing them to map their spreadsheet columns to that of the database
Since you have their spreadsheet column names, and you have the database column names, then this problem is essentially a UI/UX problem. You need to show both lists, and allow the user to map them. I can imagine several approaches to this. My first thought would be some sort of drag/drop operation, as follows:
Create a list of boxes, one for each field in your database table, and include the field name in (or above) the box. I'll call this the db field list. Then, create another list for each column from the spreadsheet, which I'll call the spreadsheet column list. The user would drag/drop items from the spreadsheet column list to the db field list.
When a mapping has been completed by the user, you would store the column/field names in as data for the DOM element of the db field list box. Then upon submission, you would acquire the mapping data by visiting each box and adding it to an array. Then you would serialize that array into JSON and send that to your form submission handler.
This could be difficult or easy, depending on your knowledge of UI implementations using JavaScript. jQuery makes this easy (if you know jQuery). There's even a jquery UI plugin that does this: https://jqueryui.com/droppable/.
A quick search for javascript drag drop would help, and here's a few articles I found:
https://www.w3schools.com/html/html5_draganddrop.asp
https://medium.com/quick-code/simple-javascript-drag-drop-d044d8c5bed5
You would also need to submit the array of mappings using javascript. You could search for that as well, and here's an article I found:
https://codereview.stackexchange.com/questions/94493/submit-an-array-as-an-html-form-value-using-javascript

Keep track of all objects created using a form

Ok, this might take a bit of explanation.
I have a class called PurchaceOrder, which has a form with 18 fields. A lot of the time, there will be several PurchaceOrders with the first 12 fields being the same. To facilitate this (and remove the chance of a user making an error when trying to make the 12 fields the same every time) I have a form that allows the user to add more than one PurchaceOrder with the first 12 Fields being carried over from one entry to the next. This is all fine and good, and is working very well.
However; I need a splash page after the user is done adding all of his/her PurchaceOrders, that shows all the entries that were just made. This means I need to track the new entries that are being created, but I can't think of a way to do this. For now I'm just filtering the categories, so that the PurchaceOrders with the first 12 fields being the same as the ones just entered are displayed, but this obviously won't really work (there could be a previous entry that has those 12 fields that are the same). What I'd really like to have is a list of Primary Keys of the entries that were just created. I could then pass this information onto the view that's responsible for the confirmation landing page.
In my current view I have the following:
if form.is_valid():
entry=form.save()
My thinking was that I could then do something like:
pks = [pks, entry.id]
I don't know where I would instantiate the list. If I did it in the view, it would be whipped out every time the page was reloaded.
I'd appreciate any help.
My first thought would be to have a separate class/method/function that keeps track of the PurchaceOrder's in each form. The ID could be tied to the result through a database or array/linked list that is auto-incremented when a new PurchaceOrder is created.
As far as instantiating the list, have it instantiated when the class is first called, or have it as a separate class. After writing that, a separate class makes more sense, which is probably why I posted it that way originally.
After leaving this alone for a long time, I've finally come up with a suitable solution. What I've done is created a url conf that has a regex of (?P<match>/+)/in it. That rexeg will be used to contain an arbitrary number of primary keys separated by /. I use this to store all of the primary keys of the PurchaseOrders that have been created (that is, when I create PurchaseOrder number 15, it redirects me to /new/15, then when I create number 16, it directs me to /new/15/16/, and so on. Then when I'm ready to confirm, I'm sent to /confirm/15/16/ and I can easily access the pk's of the entries I've just created by calling pks=match.split('/'). I can then simply iterate through pks and do something for each object. I think it's an elegant solution, as long as you don't mind your user seeing the primary keys in the url.

Django add text via button click

Using Django, I want to store a list of textfields that is initially empty but when you click a button it will add a text field. a good example is the add attachment in email, except instead of adding files i just want to add textfields.
Then once I have this feature setup I would like to be able to add all textfields into one textfield to allow a user to edit all textfields at one time, then parse them back into the original text field.
is this possible?
Thank you
cooper
If you don't need to add an unlimited number of inputs, my recommendation would be to use the combination of a custom MultiValueField and MultiWidget, and show/hide the fields you might want to initially hide.
You'll be able to leverage the built-in compress and decompress methods to take the values and split them back out to individual inputs (you'd have to split the text on a delimiter) and then take the separate values and compress them back into one.
If you need to be able to add an unlimited number of fields, and parse them back into one value, I would use a formset to hold the values, which you can concatenate the values of back into another field if you need, or just show in a loop, if all you need is to display them as if they were one field.

Looping through data over multiple pages in Django

I'm trying to find the best way to go about my problem and I would love your input. I am trying to allow users to scan multiple barcodes into a text area. After they are submitted they are split into an array. The user then inputs how many iterations of each value in the array are to be inserted into a MySQL database. I've achieved this using PHP and session variables, looping through the array one step at a time. With Django I've found it a little more difficult and I am wondering if I should just have a "temporary" table in my database that gets refilled with the values from the array of barcodes. The following pages then pull each value from the table instead of using any sort of session variables.
Edit:
I apologize for the confusing question. Let me try and clear it up a bit:
I need to render a view based on each value in the user-submitted array. When it is first submitted, a view is rendered for the first value. When the user hits "Next" a view will be rendered for the second value in the array, and so on.
As for the database issue, each value can have two "types." The user will declare how many of each type is added to the database in each of the views I am trying to render.
Thank you.
this is nothing about django.
forget that temporary table.
add a field "filled" to ur table
select 1st not-filled row, and show "refill" page by this row
then update user input number back to db, set "filled" to "true" at same time.
You probably can port your PHP solution using a Django session object.
I'm not sure if that "one item at a time" is a feature or a "it was easier to code that way" thing, but in the second case - you may want to use Django Formsets to display all items at once and avoid looping through the array.

Django Form 2 Stages

I'm trying to make a Django order system that requires 2 forms. The first form, lets users choose some some quantities and basic contact info some. Then, using the quantities of each item they ordered I generate a 2nd form which allows them to choose some options for each item. This system is specifically for event tickets. Here are the 2 stages:
1) Get the order info such as name, address, phone of the person placing the order. Also find out how many people are coming to each of the possible events.
2) Based on the number of people per event, get their name and e-mail address.
I already have both forms created. I am just getting tripped up in the views. When they submit form 1, I need to take that info and save some of it and then send them to form 2. At form 2, they will fill out the rest of the info and finish processing.
How would you set up the views in such as case? I essentially nee-d to call on view form another and pass data between. I tried using kwargs, but I have trouble processing the second form.
Without seeing your models, it's hard to give an exact solution, but one approach is to have two separate views, one for each form.
Once you've processed the first form, you're most likely going to have an instance of some object that you created from the first form. It sounds like you just need to pass the id of that object to your next view where you could then get that object and do whatever association you need.
Also, it sounds like you might need to be collecting data from several instances of a form...
(2) Based on the number of people per event, get their name and e-mail
address.)
You'll want to use a formset for that.