作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
可以更新 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/
我是一名优秀的程序员,十分优秀!