Models.py:
class ItemImage(Model):
image = ImageField(upload_to=get_user_upload_path)
Forms.py:
class ItemImageForm(ModelForm):
class Meta:
model = ItemImage
Say, I initialized an ItemImageForm with an instance of ItemImage and added the form as img_form to the context used for rendering the template with. I know that I can access the corresponding ItemImage instance with img_form.instance. But I am curious if there’s a more direct way to access ItemImage.image from its corresponding img_form.image?
It shouldn’t be accessed at all. The form consists of initial data and cleaned data. If you refer to initial data, use
instance. If you need value after user’s action, callis_validorfull_cleanand later look for value incleaned_data. Everything in between is hacking.If you want to perform any special cleaning on
img_form.image, createclean_imagemethod, but you’re probably aware of that already.