我在 Ubuntu 18.04 上使用 Django 3.8.2,我在保存 django 模型時生成一個 PDF 檔案,但檔案是無限回圈地生成的。
我有這個 Django 模型:
class Fattura(models.Model):
ordine = models.ForeignKey(Ordine, on_delete=models.CASCADE, related_name="fatture", null=False)
pdf = models.FileField(upload_to='archivio/fatture/%Y/%m')
該pdf欄位是在保存實體時根據相關“ordine”欄位中包含的資訊生成的,該欄位是另一個模型的外鍵:
class Ordine(models.Model):
utente = models.ForeignKey(User, on_delete=models.CASCADE, related_name="ordini", null=False)
data = models.DateField(auto_now_add=True)
abbonamento = models.ForeignKey(Abbonamento, on_delete=models.PROTECT, null=False)
importo = models.DecimalField(max_digits = 5, decimal_places = 2, null=False)
我save()在我的 Fattura 模型的方法中宣告了生成 PDF 的每一條指令。我正在使用 Django 檔案推薦的庫:reportlab。這是自定義保存方法:
def save(self, *args, **kwargs):
self.count_save = 1 # I defined this attribute which I increment to understand what is actually looping
print("COUNT SAVE: " str(self.count_save)) # it always grows, that's the save method being re-called
if self.pdf._file == None:
try:
buffer = io.BytesIO()
p = canvas.Canvas(buffer)
p.drawString(100,100, str(self.ordine))
p.showPage()
p.save()
buffer.seek(0)
utente = self.ordine.utente
num_questa_fattura = utente.ordini.count()
nome_file = "{}_{}-{}.pdf".format(
self.ordine.utente.first_name.lower(),
self.ordine.utente.last_name.lower(),
num_questa_fattura)
percorso = '{}/upload/archivio/fatture/{}/{}/{}'.format(
BASE_DIR, # from settings.py
self.ordine.data.year,
self.ordine.data.month,
nome_file)
file_temporaneo = NamedTemporaryFile(delete=True)
file_temporaneo.write(buffer.getbuffer())
file_temporaneo.flush()
temp_file = File(file_temporaneo, name = nome_file)
print(nome_file)
print(file_temporaneo)
print("--------------------- SAVE")
self.pdf.save(nome_file, file_temporaneo, save=True) # saving the field
file_temporaneo.close()
except:
raise ValidationError("Invoice could not be saved")
super().save(*args, **kwargs) # saving the object to the database
當我使用 Django 管理面板保存 Fattura 物件時,會在我宣告的檔案夾內回圈生成數百個 PDF 檔案,并且沒有真正完成資料庫保存。之后沒有可用的 Django 模型物件,因此最終沒有任何內容保存到資料庫中,并且每次生成精確的 499 個檔案。
我不確定是什么導致了這個回圈,也許是try陳述句中的 pdf 欄位保存方法?還是決賽super().save(*args, **kwargs)?我無法洗掉它,否則如果 PDF 檔案實際上與一個實體相關聯并且我正在更新另一個欄位,則不會保存任何內容ordine
這是我的日志的摘錄
[Tue Nov 02 15:25:06.974768 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] COUNT SAVE: 262
[Tue Nov 02 15:25:07.010584 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] maria_rosselli-1.pdf
[Tue Nov 02 15:25:07.015778 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] <tempfile._TemporaryFileWrapper object at 0x7f7b07ac0390>
[Tue Nov 02 15:25:07.020944 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] --------------------- SAVE
[Tue Nov 02 15:25:07.124451 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] COUNT SAVE: 263
[Tue Nov 02 15:25:07.164934 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] maria_rosselli-1.pdf
[Tue Nov 02 15:25:07.170144 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] <tempfile._TemporaryFileWrapper object at 0x7f7b07ac0e48>
[Tue Nov 02 15:25:07.175250 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] --------------------- SAVE
[Tue Nov 02 15:25:07.253426 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] COUNT SAVE: 264
[Tue Nov 02 15:25:07.286589 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] maria_rosselli-1.pdf
[Tue Nov 02 15:25:07.291734 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] <tempfile._TemporaryFileWrapper object at 0x7f7b07ac0e10>
[Tue Nov 02 15:25:07.296894 2021] [wsgi:error] [pid 10050] [remote 95.x.x.x:36048] --------------------- SAVE
uj5u.com熱心網友回復:
您需要將 save 設定為 False 以在保存 pdf 時再次呼叫模型實體的保存方法:
self.pdf.save(nome_file, file_temporaneo, save=False)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345597.html
