type object 'object' has no attribute '_meta'

i just wanna have a similar logentry like history in admin page in my own page instead of admin pages. and now it just shows ‘type object ‘object’ has no attribute ‘_meta’’.

view.py

    res = dict()
    res['objects'] = LogEntry.objects.all()  
    LogEntry.objects.log_action(
        user_id=request.user.pk,  
        content_type_id=get_content_type_for_model(object).pk, 
        object_id=object.pk, 
        object_repr=object.__repr__(),
        action_flag=CHANGE,
        change_message=''oooops"
    )
    return render(request, 'homePage.html', {"objects": object})

html

<tbody>
                        {% for i in objects %}
                        <tr>
                            <td>{{ i.action_time }}</td>
                            <td>{{ i.object_repr }}</td>
                            <td>{{ i.object_id }}</td>
                            <td>{{ i.get_action_flag_display }}</td>
                            <td>{{ i.user }}</td>
                            <td>{{ i.change_message }}</td>
                        </tr> {% endfor %}
                    </tbody>

When requesting assistance with an error.

  • Please post the complete error message with the traceback (posted as preformatted text, just like code)
  • Post the complete view and templates involved

You have mismatched quotes here. You have two apostrophes (') at the beginning of oooops instead of a quote character (").

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 5.1.6
Python Version: 3.10.5
Installed Applications:
[‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘Bishe.apps.BisheConfig’,
‘helloWorld.apps.HelloworldConfig’]
Installed Middleware:
[‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’]

Traceback (most recent call last):
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\auth\decorators.py”, line 60, in _view_wrapper
return view_func(request, *args, **kwargs)
File “E:\DEV Tools\Workspace\djangoProject\Bishe\views.py”, line 25, in homePage
content_type_id=get_content_type_for_model(object).pk, # 对应数据库Model的id
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\admin\options.py”, line 93, in get_content_type_for_model
return ContentType.objects.get_for_model(obj, for_concrete_model=False)
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\contenttypes\models.py”, line 41, in get_for_model
opts = self._get_opts(model, for_concrete_model)
File “E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\contenttypes\models.py”, line 29, in _get_opts
return model._meta

Exception Type: AttributeError at /
Exception Value: type object ‘object’ has no attribute ‘_meta’

these are the full error message.
and thx for finding bug in change_message .i’ve gotten it right :slight_smile:

This is the summary page shown in the browser. It’s not as complete or as helpful as the traceback provided on the console.

And, we still need to see the complete view and template.

and if this error is related to Django version? i searched some tutorials they are using object_repr, but in IDE it just shows no such Unresolved Reference object_repr.

errors in console

Internal Server Error: /
Traceback (most recent call last):
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\auth\decorators.py", line 60, in _view_wrapper
    return view_func(request, *args, **kwargs)
  File "E:\DEV Tools\Workspace\djangoProject\Bishe\views.py", line 25, in homePage
    content_type_id=get_content_type_for_model(object).pk,  # 对应数据库Model的id
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\admin\options.py", line 93, in get_content_type_for_model
    return ContentType.objects.get_for_model(obj, for_concrete_model=False)
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\contenttypes\models.py", line 41, in get_for_model
    opts = self._get_opts(model, for_concrete_model)
  File "E:\DEV Tools\Workspace\djangoProject\venv\lib\site-packages\django\contrib\contenttypes\models.py", line 29, in _get_opts
    return model._meta
AttributeError: type object 'object' has no attribute '_meta'
[28/Apr/2025 04:07:03] "GET / HTTP/1.1" 500 86315

html

<table class="table table-bordered" style="width: 60%;margin-left: 20%;text-align: center">
                    <caption style="text-align: center">
                    <span style="font-size: large;color: black">【<span style="color: pink">{{ client.name }}</span> </span>
                    </caption>
                    <thead>
                        <tr style="background-color: #cdd8e5">
                        <th style="width: 100px;text-align: center">操作时间</th>
                        <th style="width: 200px;text-align: center">操作对象</th>
                        <th style="width: 50px;text-align: center">对象id</th>
                        <th style="width: 100px;text-align: center">动作标志</th>
                        <th style="width: 100px;text-align: center">操作用户</th>
                        <th style="width: 300px;text-align: center">修改内容</th>
                    </tr>
                    </thead>
                    <tbody>
                        {% for i in objects %}
                        <tr>
                            <td>{{ i.action_time }}</td>
                            <td>{{ i.object_repr }}</td>
                            <td>{{ i.object_id }}</td>
                            <td>{{ i.get_action_flag_display }}</td>
                            <td>{{ i.user }}</td>
                            <td>{{ i.change_message }}</td>
                        </tr> {% endfor %}
                    </tbody>
                </table>

view

def homePage(request):
    res = dict()
    res['objects'] = LogEntry.objects.all()
    LogEntry.objects.log_action(
        user_id=request.user.pk, 
        content_type_id=get_content_type_for_model(object).pk,  
        object_id=object.pk,  
        object_repr=object_repr, 
        action_flag=CHANGE, 
        change_message='ooops' 
    )
    return render(request, 'homePage.html')

The variable named “object” isn’t defined in this function.

thx a lot. i have known what i need to do.

btw, may i have your idea about how to achieve my purpose about showing the operation history in my own page?

You would need to provide specific details about what exactly it is you’re looking to do here.

(Just referring to the “admin’s operation history” doesn’t provide nearly enough information about what you’re looking to show. For example, are you just looking to dump all admin activity? Activity on specific models? Are you looking to track activities performed outside the admin?)

yes, i just want to have a form to show all users’ activity like auth change or delete a account with their name, id ,when did this activity.
it’s quite simple to show in admin page, but i got no idea about how to do it in my own page :frowning:

The LogHistory model is a model like any other. You would query it and render the results of the query exactly like any other model.