Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't set ImageField url attribute
    text
    copied!<p>I want to change my ImageField's attribute, however I'm constantly getting the Can't set attribute error.</p> <p>My model is </p> <pre><code>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) </code></pre> <p>And my helper functions are :</p> <pre><code>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() </code></pre> <p>The problem is :</p> <p>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 <a href="https://stackoverflow.com/questions/4583090/django-changing-the-url-of-an-optional-imagefield">SO question</a>) But still i'm getting the same result, the path doesn't change, and gives the "can't set attribute" response. </p> <p>Any idea ??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload