NameError: name 'model_object' is not defined

740 views Asked by At

I'm trying to get all model operations in my django app with the following code but it returns an error. I get the error

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})
1

There are 1 answers

1
rahul.m On

try this

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-contenttypemanager