I have the following models:
class Pic(models.Model):
name = models.CharField(max_length=50)
img = ImageField(upload_to='images/', default=None, null=True, blank=True)
class InputTest(models.Model):
pic_id = models.ForeignKey(Pic, on_delete=models.CASCADE)
image_field_crop = CropperImageField(upload_to='images/', default=None, null=True, blank=True)
When adding a new InputTest object, the admin selects a PicID Object (foreign key) from the existing ones. I want right after selecting it, the object to save and refresh.
For example:
When adding a new InputTest object, I select PIC#1 as foreign key. The program will run this overwritted method:
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): super(InputTest, self).save(force_insert, force_update, using, update_fields) self.image_field_crop = self.pic_id.img super(InputTest, self).save(force_insert, force_update, using, update_fields)
Then when the pages refreshes, the image_field_crop is auto-completed with the PIC#1.img and I can use it in CropperJS (using CropperImageField)
Is it possible to do this? To auto-save the object and refresh the page right after selecting the pic_id when adding a new InputTest.
Hi you can use below save method in InputTest model after you select a pic_id in InputTest admin and select save button image_field_crop filed filled automatically
def save(self, *args, **kwargs):
self.image_field_crop = self.pic_id.img
super(InputTest, self).save(*args, **kwargs)
I hope it helps you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.