Django add dynamic fields to front end and then insert it to Model

I have a Model which consists of almost 500+ fields. I want to take data through the front-end Form. But the issue is I can’t display all the 500 fields.

what I want is I will add the fields which I need. like on day-1 I will add 3 fields. food = 230 , petrol = 500, rent = 700. on the second day, fields may vary. I can add 20 fields on the second day. on the third day data may be of different fields. like petrol = 200, cement= 5000 , steel = 100 etc etc and so on!

Simple Solution is: I displayed all fields in HTML and submitted it to view and received in View function through Post Request! and then save() . But i want it to be dynamic!

if request.method == 'POST':
        print("this is post")
        # print()
        labour_expense = request.POST['labour']
        equipments_expense = request.POST['equipments']
        rent_expense = request.POST['rent']
        cement_expense = request.POST['cement']
        steel_expense = request.POST['steel']
        other1_expense = request.POST['other1']
        other2_expense = request.POST['other2']
        other3_expense = request.POST['other3']
        ..... .... fields upto 500+ 
        # expense_paidby = request.POST['paid_by']
        expense_paidby_id = request.POST['paid_by']
        paidby= PaidBy.objects.get(id=expense_paidby_id)
        # expense_paidby = PaidBy.objects.get(name__icontains = request.POST['paid_by'])
        projectname_id= request.POST['pname']
        # project_in_which_expenses_made = request.POST['project_name']
        date = request.POST['date']
        # total = food_expense+petrol_expense+easyload_expense+labour_expense+equipments_expense+rent_expense
        # print(petrol_expense, projectname_id , project_in_which_expenses_made)
        inst= ProjectExpenses(food=food_expense,petrol=petrol_expense,easyload=easyload_expense ,labour=labour_expense ,
                              equipments=equipments_expense ,rent=rent_expense,cement=cement_expense,steel=steel_expense,
                              other1=other1_expense, other2=other2_expense, other3=other3_expense, date_of_expense=date, PaidByName=paidby,
                              expenses_in_project = project_in_which_expenses_made)
        #in query i will have 500 fields.

what I want is how I can create dynamic input fields with name attributes as I have in the database.

how should I create these fields dynamically in the front end and then how I will handle it in view function instead of writing all 500+ fields through “value = request. post[“500+ fields line by line”]” means how i can handle it dynamically only those fields which come to view function. and then save it to DB.

data will only insert in selected fields all remaining will be zero by defult.

I will appreciate forum Help.

What if, instead of a 500+ field ProjectExpenses model, you have two smaller models: Expense and ExpenseType.

ExpenseType would be a list of all the kinds of expenses, so it would have an entry for food, petrol, other, etc. Your “Select Expense Name” drop-down could be driven from this list.

Expense would be the record of a single expense. It would have the expense type, amount, paid by, project name/id, and date. Your “Add Expenses” form would map nicely to a single Expense.

When you add an expense via the form, you POST a single new Expense record to the database. No more worrying about what to do about 500+ other fields.

An added benefit is that you wouldn’t need to keep adding fields like “other4”, “other5”, etc. The user could add any number of “other” expenses.

This might also make querying a bit easier. You can ask your database questions like “what is the total amount of food expenses for project x?” rather easily.


It sounds interesting.

But I have some questions. in this way I will have to insert one record at a time? like in food =200, i will click on insert. then next time if I have to insert another record in petrol = 400 it will be inserted separately?

in the end how I will display all the same day records in a single row with all the fields like the below table?

Yes, each Expense record would be inserted one at a time. If your UI changes and you allow users to enter multiple expenses at once, that’s fine too, but on the back end you would be inserting multiple Expense records, one for each expense entered by the user.

Because each Expense record keeps track of its date, associated project, paid by, etc., you can group the expenses however you want for a report like the “NAF Daily Expenses” report. It might be easiest to gather the data for the report on the back end, and then the front end can display the data as a table or however it wants.

one last question!

if i insert data for food= 200 on date 24/7/2021 , and then i insert petrol = 500 on same date, and then steel = 1000 on same date. this data will be in separate rows?

How i will display same day records in one row(table format) like my above image!

The expense data would be in separate rows in the database, but you can display that data however you want on the front end. Your view would gather all the necessary data from your database via your models, for example it could get all of the expenses, group them by date and project, and then each group’s expense data could be used to populate an object that represents a row in your table. Your view would feed the list of these row objects to your template, which would iterate over the list, creating a table row for each object.

I implemented your solution and its looking Awesome. Please check it and give me feedback on it. Thanks <3

Front End!

Looking good. Do you still need to display all the same-day records in a single row?

1 Like

The application is almost completed thanks for your help. I am going to upload it to the server.

Which server is best for an application like this heroku will work?

I’m sure Heroku is fine :slightly_smiling_face: