我試圖更新多行取決于多個資料。但是當我提交時,只有一行更新并得到這個錯誤'int' object has no attribute 'save'。我認為問題在我看來。我的觀點如下:
def saleForm(request):
stb = []
if request.method == 'POST':
for stb_array in request.POST.getlist('stb_sl'):
stb.append(stb_array)
i = 0
for stb_ho in stb:
stb_update = StpDetails.objects.filter(stp_sl=stb_ho).update(
sale_date=date,
sales_invoice=invoice,
status='Sold')
stb_update.save()
i = 1
messages.success(request, 'You have successfully save data.')
return render(request,'sale/invoice_details.html',{})
我的型號:
class StpDetails(models.Model):
id = models.AutoField(primary_key=True)
entry_date = models.DateField(default=date.today)
stp_sl = models.CharField(max_length=50, unique=True)
stp_name = models.CharField(max_length=30)
sale_date = models.DateField(auto_now=False, null=True)
sales_invoice = models.CharField(max_length=20, blank=True)
install_date = models.DateField(auto_now=False, null=True)
installer = models.CharField(max_length=50, blank=True)
house = models.CharField(max_length=100, blank=True)
status = models.CharField(max_length=7, default='Unsold')
def __str__(self):
return self.stp_sl
uj5u.com熱心網友回復:
該方法的檔案QuerySet.update()顯示該方法已經保存了更改,因此.save()以后無需使用。
此外,此方法不回傳模型實體,而是一個整數;這就是為什么您會收到錯誤訊息,因為它stb_update是一個整數并且沒有名為.save().
update() [...] 并回傳匹配的行數(如果某些行已經具有新值,則可能不等于更新的行數)。
所以你的代碼可能是這樣的:
def saleForm(request):
if request.method == 'POST':
date = ...
invoice = ...
stb = []
for stb_array in request.POST.getlist('stb_sl'):
stb.append(stb_array)
i = 0
for stb_ho in stb:
stb_update = StpDetails.objects.filter(stp_sl=stb_ho).update(
sale_date=date,
sales_invoice=invoice,
status='Sold')
i = 1
messages.success(request, 'You have successfully save data.')
else:
# a GET request
pass
return render(request, 'sale/invoice_details.html', {})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/317113.html
標籤:姜戈 PostgreSQL django-models
上一篇:如何在pl/pgsql中的列陣列上創建具有條件的函式?
下一篇:按成員為每個組設定不同的過濾器
