gpt4 book ai didi

Django - 如何更新模型 save() 方法中的字段?

转载 作者:行者123 更新时间:2023-12-01 05:25:10 25 4
gpt4 key购买 nike

可以更新 save() 方法中的模型字段吗?

模型.py:

class BicycleAdItemKind(MPTTModel):

image_file_temp_fullpath = ""
image_file_temp_filename = ""

def url(self, filename):
#pdb.set_trace()

#url = "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, filename)
url = "MultimediaData/HelpAdImages/ItemKind/Temp/%s" % (filename)
self.image_file_temp_fullpath = url
self.image_file_temp_filename = filename
return url

def item_kind_image(self):
return '<img align="middle" src="/media/%s" height="60px" />' % self.image
item_kind_image.allow_tags = True

n_item_kind = models.CharField(max_length=50) # Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
description = models.TextField(null=True, blank=True)
image = models.ImageField(upload_to=url, null=True, blank=True)
date_inserted = models.DateTimeField(auto_now_add=True)
date_last_update = models.DateTimeField(auto_now=True)

def save(self, *args, **kwargs):
if not self.id:
BicycleAdItemKind.tree.insert_node(self, self.parent)
super(BicycleAdItemKind, self).save(*args, **kwargs)

pdb.set_trace()
# I will move the file from "Temp" folder to the folder with the "Id" number
from django.core.files.move import file_move_safe
src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath
dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
if not os.path.exists(new_directory):
os.makedirs(new_directory)

if file_move_safe(src, dst):
# I will update the field image
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
# Delete the Temp file

def __unicode__(self):
return self.n_item_kind

class MPTTMeta:
order_insertion_by = ['n_item_kind']

下面的代码行不执行任何操作,
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)

可以在保存方法中进行更新吗?

给我一些线索。

此致,

最佳答案

将 super 保存移动到 save() 的末尾,以便在您进行更改后更新模型。

def save(self, *args, **kwargs):
if not self.id:
BicycleAdItemKind.tree.insert_node(self, self.parent)

pdb.set_trace()
# I will move the file from "Temp" folder to the folder with the "Id" number
from django.core.files.move import file_move_safe
src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath
dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
if not os.path.exists(new_directory):
os.makedirs(new_directory)

if file_move_safe(src, dst):
# I will update the field image
BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
# Delete the Temp file

super(BicycleAdItemKind, self).save(*args, **kwargs)

关于Django - 如何更新模型 save() 方法中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com