在保存模型之前,我会重新调整图片大小。但是,如何检查是否添加了新图片或更新了描述,以便每次保存模型时都可以跳过重新缩放?
class Model(model.Model):
image=models.ImageField(upload_to='folder')
thumb=models.ImageField(upload_to='folder')
description=models.CharField()
def save(self, *args, **kwargs):
if self.image:
small=rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
super(Model, self).save(*args, **kwargs)
我想仅在新图像加载或图像更新时重新缩放,而不是在更新描述时重新缩放。
答案 0 :(得分:115)
一些想法:
class Model(model.Model):
_image=models.ImageField(upload_to='folder')
thumb=models.ImageField(upload_to='folder')
description=models.CharField()
def set_image(self, val):
self._image = val
self._image_changed = True
# Or put whole logic in here
small = rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
def get_image(self):
return self._image
image = property(get_image, set_image)
# this is not needed if small_image is created at set_image
def save(self, *args, **kwargs):
if getattr(self, '_image_changed', True):
small=rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
super(Model, self).save(*args, **kwargs)
不确定它是否适用于所有伪自动django工具(例如:ModelForm,contrib.admin等)。
答案 1 :(得分:13)
检查模型的pk字段。如果它是None,那么它是一个新对象。
class Model(model.Model):
image=models.ImageField(upload_to='folder')
thumb=models.ImageField(upload_to='folder')
description=models.CharField()
def save(self, *args, **kwargs):
if 'form' in kwargs:
form=kwargs['form']
else:
form=None
if self.pk is None and form is not None and 'image' in form.changed_data:
small=rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
super(Model, self).save(*args, **kwargs)
编辑:我在form.changed_data中添加了对'image'的检查。这假设您使用管理网站更新图像。您还必须覆盖默认的save_model方法,如下所示。
class ModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.save(form=form)
答案 2 :(得分:5)
您可以提供额外的参数来确认发布新图像 类似的东西:
def save(self, new_image=False, *args, **kwargs):
if new_image:
small=rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
super(Model, self).save(*args, **kwargs)
或传递请求变量
def save(self, request=False, *args, **kwargs):
if request and request.FILES.get('image',False):
small=rescale_image(self.image,width=100,height=100)
self.image_small=SimpleUploadedFile(name,small_pic)
super(Model, self).save(*args, **kwargs)
我认为这些不会在简单调用时破坏你的保存。
您可以将它放在您的admin.py中,以便这也适用于管理站点(对于上述第二个解决方案):
class ModelAdmin(admin.ModelAdmin):
....
def save_model(self, request, obj, form, change):
instance = form.save(commit=False)
instance.save(request=request)
return instance
答案 3 :(得分:2)
我为实现目标所做的是制作这个......
# I added an extra_command argument that defaults to blank
def save(self, extra_command="", *args, **kwargs):
并且在save()方法下面是这个..
# override the save method to create an image thumbnail
if self.image and extra_command != "skip creating photo thumbnail":
# your logic here
所以当我编辑某些字段而不是编辑图像时,我把它放了..
Model.save("skip creating photo thumbnail")
您可以将"skip creating photo thumbnail"
替换为"im just editing the description"
或更正式的文字。
希望这个有帮助!
答案 4 :(得分:1)
在数据库中查询具有相同PK的现有记录。比较新图像和现有图像的文件大小和校验和,看它们是否相同。
答案 5 :(得分:0)
在新版本中,它是这样的:
def validate(self, attrs):
has_unknown_fields = set(self.initial_data) - set(self.fields.keys())
if has_unknown_fields:
raise serializers.ValidationError("Do not send extra fields")
return attrs
答案 6 :(得分:0)
我找到了另一种将数据存储到数据库中的简单方法
models.py
class LinkModel(models.Model):
link = models.CharField(max_length=500)
shortLink = models.CharField(max_length=30,unique=True)
在数据库中,我只有2个变量
views.py
class HomeView(TemplateView):
def post(self,request, *args, **kwargs):
form = LinkForm(request.POST)
if form.is_valid():
text = form.cleaned_data['link'] # text for link
dbobj = LinkModel()
dbobj.link = text
self.no = self.gen.generateShortLink() # no for shortLink
dbobj.shortLink = str(self.no)
dbobj.save() # Saving from views.py
在此我只在views.py中创建了模型实例,并将数据从视图中放入/保存为2个变量。