无法设置 ImageField url 属性

发布于 2024-10-20 12:26:53 字数 1704 浏览 3 评论 0原文

我想更改 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()

问题是:

我想将我的社会目录保存为 Society_society_id。但通常情况下,在保存模型之前我无法分配任何 id。所以我正在创建一个 tmp 文件,其名称是时间值。然后为了到达 Society 文件夹,我想重命名该文件。因此,我的 fix_avatar 只是在保存协会后将 tmp 文件的内容移动到 Society_(society_id) 文件夹。到目前为止一切都很好。但是,我的协会的 ImageField 仍然保存着之前创建的文件夹。为了改变它的值,我发现我可以使用干净的方法。(来自此 SO问题)但我仍然得到相同的结果,路径没有改变,并给出“无法设置属性”响应。

有什么想法吗?

I want to change my ImageField's attribute, however I'm constantly getting the Can't set attribute error.

My model is

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)

And my helper functions are :

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()

The problem is :

I want to save my society directories as society_society_id. But normally, I can't assign any id before the model is saved. So i'm creating a tmp file whose name is a time value.Then to reach societies folder, I want to rename this file. So, my fix_avatar simply moves the tmp file's content to society_(society_id) folder after the society is saved. So far so good everything works well. However, my society's ImageField still holds the previously created folder. In order to change it's value, I found that i can use clean method.(from this SO question) But still i'm getting the same result, the path doesn't change, and gives the "can't set attribute" response.

Any idea ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-10-27 12:26:53

不确定自从提出这个问题以来 Django 中是否有过改变。关于此不可能的票证仍然存在: https://code.djangoproject.com/ticket/15590

然而,您实际上可以通过这样做来更改路径:

self.avatar = 'uploads/example/path/'

这项工作还有什么作用:

self.avatar.name = 'uploads/example/path/'

它在很多情况下都对我们有用。

Not sure, if this was ever changed in Django since this question was asked. A ticket about this not being possible still exists: https://code.djangoproject.com/ticket/15590

However, you actually can change the path by doing it like this:

self.avatar = 'uploads/example/path/'

What also does the job:

self.avatar.name = 'uploads/example/path/'

It has worked for us in several occasions.

话少心凉 2024-10-27 12:26:53

问题在于:

self.avatar.path = get_society_path(self,self.avatar.path)

您无法更改 FileField/ImageField 实例中路径属性的值,它是只读的。有一个 提案 在 Django 1.4 中更改此设置

The problem is here:

self.avatar.path = get_society_path(self,self.avatar.path)

You cannot change the value of the path attribute in FileField/ImageField instances, it's readonly. There is a proposal to change this in Django 1.4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文