ImageField の属性を変更したいのですが、常に属性を設定できませんというエラーが発生します。
私のモデルは
class Society(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
summary = models.TextField(blank=True,null=True)
members = models.ManyToManyField(User,null=True,blank=True)
gallery = models.ForeignKey(Gallery,null=True,blank=True)
avatar = models.ImageField(upload_to=get_society_path)
def save(self,*args,**kwargs):
super(Society, self).save(*args,**kwargs)
fix_avatar_path(self)
def clean(self):
if self.id:
self.avatar.path = get_society_path(self,self.avatar.path)
save_thumb(self.avatar.path)
そして、私のヘルパー関数は次のとおりです。
def get_society_path(instance,filename):
seperator_val = instance.id
if seperator_val is None:
seperator_val = get_time()
return '%s/society_%s/%s' % (settings.UPLOAD_ROOT,seperator_val,time_to_name(filename))
def fix_avatar_path(instance):
org_society_path = get_society_path(instance,instance.avatar.name)
make_upload_dir(org_society_path)
move(instance.avatar.path,org_society_path)
os.rmdir(os.path.dirname(instance.avatar.path))
instance.clean()
問題は :
協会のディレクトリを social_society_id として保存したいと考えています。しかし、通常、モデルを保存する前に ID を割り当てることはできません。そのため、名前が時間値である tmp ファイルを作成しています。次に、societies フォルダーに到達するために、このファイルの名前を変更したいと思います。したがって、社会が保存された後、私の fix_avatar は単に tmp ファイルの内容を social_(society_id) フォルダーに移動します。これまでのところ、すべてがうまく機能しています。ただし、私の社会の ImageField には、以前に作成したフォルダーがまだ保持されています。その値を変更するために、私はきれいな方法を使用できることがわかりました。 .
何か案が ??