Обновление данных в django без формы
Я выполняю CRUD операции без формы. Я хочу обновлять данные по их id. Мои данные обновляются, но я получаю данные в дополнительных бекетах, как это
response: {'customer_id': 2, 'customer_name': "('Shweta',)", 'pin': "('400456',)", 'city': "('DSAA',)", 'штат': "('Maharashtra',)", 'страна': "('India',)", 'customer_address': "('India',)", 'contact_person_name': "('8097998823',)", 'phone': "('7418529634',)", 'email': "('info.technicrafts@gmail.com',)", 'GST': "('741852',)", 'bill_to': "('Thane',)", 'ship_to': "('Thane',)", 'remarks': "('NA',)"}
views.py
def updateCustomer(request,id):
customer=Customer.objects.get(pk=id)
response = model_to_dict(customer)
print ("response:" , response)
if request.method == "POST":
# customer_id= request.POST['customer_id'],
customer_name= request.POST['customer_name'],
pin= request.POST['pin'],
city= request.POST['city'],
state= request.POST['state'],
country= request.POST['country'],
customer_address= request.POST['customer_address'],
contact_person_name= request.POST['contact_person_name'],
phone= request.POST['phone'],
email= request.POST['email'],
gst= request.POST['GST'],
bill_to= request.POST['bill_to'],
ship_to= request.POST['ship_to'],
remarks= request.POST['remarks'],
update_customer=Customer.objects.filter(customer_id=id).update(customer_name=customer_name,pin=pin,city=city,state=state,country=country,customer_address=customer_address,contact_person_name=contact_person_name,phone=phone,email=email,GST=gst,bill_to=bill_to,ship_to=ship_to,remarks=remarks)
print("update query set: ",update_customer)
return render(request,"customers/update-customer.html",{"customer":response})
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('view-customers', views.viewCustomers, name="view-customers"),
path('add-customers', views.addCustomers, name="customers"),
path('get-customers', views.getCustomers, name="get-customers"),
# path('update-customers', views.updateCustomer, name="update-customers"),
path('delete/<int:id>/',views.deleteCustomer, name="delete-customers"),
path('update/<int:id>/',views.updateCustomer, name="update-customers"),
]
html
Как я могу получить данные чистыми, без штрихов?
обновление без валидации - неправильный путь.
но вы можете это сделать:
def updateCustomer(request,id):
... # something, what you want
data = request.POST
customer=Customer.objects.get(pk=id)
[setattr(customer, field.name, data.get(field.name)) for field in customer._meta.fiellds if field.name in data]
customer.save()
... # something, what you want