我尝试通过在post_save中创建新对象来保存当前用户。有模型
class SourceHistory(models.Model):
source = models.ForeignKey(Source, verbose_name=u'Источник', null=False, blank=False)
editor = models.ForeignKey(User, verbose_name=u'Пользователь', null=True, blank=True)
在admin.py
中@admin.register(SourceHistory)
class SourceHistoryAdmin(admin.ModelAdmin):
list_display = ('source', 'editor')
def save_model(self, request, obj, form, change):
obj.editor = request.user
obj.save()
并创建一个新对象
@receiver(post_save, sender=Source)
def source_save_history(sender, **kwargs):
source = kwargs.get('instance')
for f in source.memorized_fields:
if source.__getattribute__(f) != source.memorized_fields[f]:
sh = SourceHistory.objects.create(
source=source
)
因此,使用这种方法,新对象中的“编辑器”字段为空。如何与当前用户一起创建SourceHistory对象?
答案 0 :(得分:0)
您不能在post_save()中执行此操作,只能在请求中确定用户