2
내 모델에서 저장 방법을 덮어 쓰고 적절한 슬러그를 만들고 이미지 필드의 복사본을 약간 수정하여 만들 것을 권합니다. 어떻게 처리 할 수 있습니까?Django 덮어 쓰기 저장
def save(self, *args, **kwargs):
super(MyModel, self).save(*args, **kwargs) #to get id
#slug
self.slug = '%s-%i' % (self.topic, self.id)
#create copy of img
cp_path = dirname(self.image.path)+'/copies_'+basename(self.image.path)
shutil.copy2(self.image.path, cp_path)
file = open(cp_path)
django_file = File(file)
django_file.name = basename(cp_path) #otherwise path will be duplicated
self.cp_image = django_file
super(MyModel, self).save(*args, **kwargs) #to save my new ImageField
create_watermark(self.cp_image, self.topic, self.text, 500, 45)
내가 슈퍼를 사용하는 원인 (MyModel, 자기) .save()를 두 번 나는 self.image 파일의 복사본을 가지고있다. 내가 볼 수 있듯이 나는 장고와 파이썬에 익숙하지 않다. 어떻게하면 좋을까요?
을 "해당 파일이나 디렉토리 :"오류 난 파일에서 작동하기 전에 나는() 메소드를 저장 사용하지 않습니다. – MilkyWay
아, 그러면 전체 저장을 post_save 핸들러로 이동하십시오. 이렇게하면 created = True가 한 번만 수행되도록 확인할 수 있습니다. –