我试图用以下代码在我的django应用程序中获取所有模型操作,但它返回了一个错误。我得到错误
NameError: name 'model_object' is not defined
def admin_main(request):
logs = LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(model_object).pk,
object_repr=unicode(obj),
object_id=obj.id,
message="",
action_flag=ADDITION if create else DELETION)
logs = logs.order_by('-action_time')[:40]
return render(request,'history.html', {"logs":logs})
尝试这个
from app.models import MyModel1 # 1st import your model
def admin_main(request):
logs = LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(MyModel1).pk,
object_repr=unicode(obj),
object_id=obj.id,
message="",
action_flag=ADDITION if create else DELETION)
logs = logs.order_by('-action_time')[:40]
return render(request,'history.html', {"logs":logs})
# e.g from doc
>>> from django.contrib.auth.models import User
>>> ContentType.objects.get_for_model(User)
<ContentType: user>
https://docs.djangoproject.com/en/4.0/ref/contrib/contenttypes/#the-内容类型管理器