我正在做没有表单的CRUD操作。我想通过id更新数据。我的数据正在更新,但在额外的后台获取数据,像这样
反应:{"customer_id":2,"customer_name":"("什维塔")","销":"("400456")","城市":"("DSAA")","状态":"("马哈拉施特拉邦")","国家":"("印度",)","customer_address":"("印度",)","contact_person_name":"(‘8097998823’)","电话":"("7418529634")","电子邮件":"("info.technicrafts@gmail.com")","消费税":"("741852")","bill_to":"("领主")","ship_to":"("领主")","备注":"("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<form method="post" >
{% csrf_token %}
<div class="row pb-2 pt-3">
<div class="col-sm mb-2">
<input type="text" class="form-control" name="customer_id" placeholder="Customer Id" value="{{customer.customer_id}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="customer_name" placeholder="Customer Name" value="{{customer.customer_name}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="pin" placeholder="Pin" value="{{customer.pin}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="state" placeholder="State" value="{{customer.state}}">
</div>
</div>
<div class="row pb-2">
<div class="col-sm mb-2">
<input type="text" class="form-control" name="city" placeholder="City" value="{{customer.city}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="country" placeholder="Country" value="{{customer.country}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="contact_person_name" placeholder="Contact Person" value="{{customer.contact_person_name}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="phone" placeholder="Phone" value="{{customer.phone}}">
</div>
</div>
<div class="row pb-2">
<div class="col-sm mb-2">
<input type="text" class="form-control" name="email" placeholder="Email" value="{{customer.email}}">
</div>
<div class="col-sm mb-2">
<input type="text" class="form-control" name="GST" placeholder="GST Number" value="{{customer.GST}}">
</div>
</div>
<div class="row pb-2">
<div class="col-sm mb-2">
<textarea type="text" class="form-control" name="customer_address" placeholder= "Customer Address">{{customer.customer_address}}</textarea>
</div>
<div class="col-sm mb-2">
<textarea type="text" class="form-control" name="bill_to" placeholder= "Bill To">{{customer.bill_to}}</textarea>
</div>
<div class="col-sm mb-2">
<textarea type="text" class="form-control" name="ship_to" placeholder= "Ship To">{{customer.ship_to}}</textarea>
</div>
<div class="col-sm mb-2">
<textarea type="text" class="form-control" name="remarks" placeholder="Remarks">{{customer.remarks}}</textarea>
</div>
</div>
<div class="row">
<div class="col-sm mb-2">
<button class="btn btn-primary" type="submit">Update Customer</button>
</div>
</div>
<br>
</form>
如何使我的数据像没有桶一样干净?
不经过验证的更新是错误的。
但是你可以这样做:
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