How to send data from list to create?
I have a problem, I would like to transfer the appropriate field parameters from one list to the form for creating something else. The names of the fields match but they are not the same in both and I would like to send only those that are the same. Example - I have a list of requests and on this list I have a button next to each record. After press the button i would like to redirect to add customer form where I will have autofill data from list request. What can I do?
def add_customer_from_list(request, pk):
application = Contact.objects.get(id=pk) #data from Contact list
form = CustomerForm(request.POST, instance=application)#to CustomerForm
if form.is_valid():
name = form.cleaned_data['name'] #same
email = form.cleaned_data['email'] #same
phone_number = form.cleaned_data['phone_number']#same
address = form.cleaned_data['address']
dog_name = form.cleaned_data['dog_name']#same
dog_age = form.cleaned_data['dog_age']
service_type = form.cleaned_data['service_type']#same (multi choice)
training_place = form.cleaned_data['training_place']
contact_date = form.cleaned_data['contact_date']
source = form.cleaned_data['source']
status = form.cleaned_data['status']
notes = form.cleaned_data['notes']
customer = Customer(name=name, email=email, phone_number=phone_number, address=address, dog_name=dog_name,
dog_age=dog_age, service_type=service_type, training_place=training_place,
contact_date=contact_date, source=source, status=status, notes=notes)
customer.save()
return redirect('xxx')
return render(request, 'xxx', {'form': form})
I try do somethin in view and templates
There are a few different ways you could approach this problem, but one possible solution is to create a mapping between the fields in the request list and the fields in the customer form. You can then use this mapping to transfer only the appropriate data from the request to the customer form when the button is pressed.
Create a dictionary to map the fields in the request to the fields in the customer form:
request_to_customer_mapping = {
'request_name': 'customer_name',
'request_email': 'customer_email',
'request_address': 'customer_address',
'request_phone': 'customer_phone'
}
In your button event handler, retrieve the data for the selected request and create a new customer object:
def on_button_press(request_id):
request_data = get_request_data(request_id)
customer_data = {}
for request_field, customer_field in request_to_customer_mapping.items():
if request_data.get(request_field):
customer_data[customer_field] = request_data[request_field]
new_customer = Customer(**customer_data)
redirect_to_customer_form(new_customer)
In this example, the on_button_press function takes the ID of the selected request as a parameter. It uses this ID to retrieve the data for the request using the get_request_data function (which you would need to implement). Then, it creates an empty dictionary to hold the data for the new customer.
It then iterates through the request_to_customer_mapping dictionary, using the keys (the request fields) to look up the corresponding values in the request data. If the field exists in the request data, it copies the value to the corresponding field in the customer data dictionary.
Finally, it creates a new customer object using the **customer_data syntax to pass the data as keyword arguments, and redirects the user to the customer form with the new_customer as parameter.
This is just one example of how you could implement this functionality, and there may be other ways to approach the problem depending on your specific requirements.
Hope this helps!
I wrote something like this but I ran into two problems. How to validate correctly in IF? And how to send a ready dictionary to the form?
def add_customer_from_list(request, pk):
request_data = get_object_or_404(Contact, id=pk)
form = CustomerForm(request.POST)
customer_data = {}
for request_field, customer_field in request_to_customer_mapping.items():
if request_data(request_field): #here is problem
customer_data[customer_field] = request_data[request_field]
new_customer = Customer(**customer_data) # and how send to form this data \/
return render(request, 'xxx', {'form': form})