[英]Save Django Model Form and refresh after selecting a Foreign Key Object
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.添加新的 InputTest object 时,管理员从现有的中选择 PicID Object(外键)。 I want right after selecting it, the object to save and refresh.
我想在选择它之后,将 object 保存并刷新。
For example:例如:
When adding a new InputTest object, I select PIC#1 as foreign key.添加新的InputTest object 时,我将 select PIC#1作为外键。 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)然后当页面刷新时,image_field_crop 会使用PIC#1.img自动完成,我可以在 CropperJS 中使用它(使用 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.
自动保存 object 并在添加新 InputTest 时选择 pic_id 后立即刷新页面。
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嗨,您可以在 InputTest model 中使用以下保存方法,在您 select 在 InputTest admin 中的 pic_id 和 select 保存按钮 image_field_crop 字段自动填充之后
def save(self, *args, **kwargs):
self.image_field_crop = self.pic_id.img
super(InputTest, self).save(*args, **kwargs)
I hope it helps you希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.