问题描述
我有两个模型,一个 CommentModel 显示为管理中 MainModel 的内联。
models.py:
class MainModel(models.Model):
customer = models.OnetoOneField(Customer,on_delete=models.CASCADE)
class CommentModel(models.Model):
main = models.ForeignKey(MainModel,on_delete=models.CASCADE,default=None)
comment_german = CharField(blank=True,default='',null=True)
comment_english = CharField(blank=True,null=True)
admin.py:
class MainModelAdmin(SimpleHistoryAdmin):
model = MainModel
inlines = [CommentModelInline]
class CommentModelInline(admin.StackedInline):
model = CommentModel
fields = ['comment_german','comment_english']
假设我想自动翻译用户在comment_german 字段中写的内容,并在MainModel 保存时将其保存到comment_english 字段中。我尝试覆盖保存方法。我成功地为另一个不是内联的模型做到了这一点。但这不适用于内联。我需要这样的东西:
def save(self,*args,**kwargs):
super().save(*args,**kwargs)
for comment in CommentModel.objects.filter(main=self):
if comment.comment_german != '' and comment.comment_english == '':
comment.comment_english = comment.comment_german.translate_to_english()
一定有办法做到这一点,也许是使用表单集,但我无法理解它。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)