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()
food_expense=request.POST['food']
petrol_expense=request.POST['petrol']
easyload_expense=request.POST['easyload']
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=ProjectName.objects.get(id=projectname_id)
# 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.
inst.save()
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.