How to implement new log_action for LogEntry in admin views - django

I'm trying to display every actions performed on my models on an admin page, using LogEntry. So far I see actions done through the admin (which is the way LogEntry works by default if I understood well), but I would like to catch actions performed through my VueJS frontend as well.
Not sure how to do this properly !
This is my code in admin.py (django 4.0)
from django.contrib import admin
from .models import *
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE
class MoniterLog(admin.ModelAdmin):
list_display = ('action_time','user','content_type','object_repr','change_message','action_flag')
list_filter = ['action_time','user','content_type']
ordering = ('-action_time',)
admin.site.register(LogEntry,MoniterLog)

Related

Unable to register the default user model with django.contrib.admin

I'm trying to manage my User table with django.contrib.admin
But during I add the User table in admin, I had an issue that doesn't appear in admin site.
Here is my code.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
#admin.register(User)
class CustomUserAdmin(UserAdmin):
pass
assert admin.site.is_registered(User) # Fails here
And when I added the other custom model, it works.
Thanks
When you modify or implement the user model, you have to be aware of AUTH_USER_MODEL.
I changed AUTH_USER_MODEL in setting.py, and it starts to work.
Thanks.

Django Custom User Model Best Practice: User = get_user_model()?

I'm trying to create a custom user class and I'd like to know the best practice for implementing referencing the new user. After following the Django docs method of implementing the custom user as follows in models.py:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
And settings.py
AUTH_USER_MODEL = 'myapp.MyUser'
And admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)
The Django docs say to use settings.AUTH_USER_MODEL in place of user specifically for ForeignKeys and OnetoOneField, but it's not clear about other instances.
My question is specific to how to refer to the custom user class in views.py. Before defining the user class I was using
from django.contrib.auth.models import User
But after defining a custom class this is no longer correct. I've seen boilerplate code use this method in the beginning of views.py:
from django.contrib.auth import get_user_model
User = get_user_model()
Is this the best practice for referencing the custom user? Or should I just be using settings.AUTH_USER_MODEL in place of where I previously had User?
Using settings.AUTH_USER_MODEL will load the user class lazily after all django apps are loaded. Calling get_user_model() on a module level when your app is initially loaded may result in that the user model app is not loaded and also circular imports.
Update: I read two specific questions:
How to correctly access the user model, contrib or custom.
Djangos get_user_model() is quite simply a call to django.apps get_model() using the settings.AUTH_USER_MODEL. If you are writing apps that might be reused in other projects with other user models, use the get_user_model call. Always. Then it doesn't matter what the user model is.
If you have created your own core.User model and is very confident that your code will only be used in this project, from core.models import User works as well.
When to use the string representation from settings instead of fetching the model.
The string representation will in the end usually call the same django.apps get_model() anyway. By giving a string instead of the class itself in Foreignkeys, OneToOneFields etc you simply don't require the model to be looked up during django app imports, where the user model may not yet be available. So using string representation is simply deferred loading of a model. The same goes for all models.
An also during djangos different major versions this behavior have changed, which is another topic. Notice that get_user_model() have been updated in Django 1.11 for import usage.
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#referencing-the-user-model
you can go with get_user_model instead User
from django.contrib.auth import get_user_model
User = get_user_model()
get_user_model will Returns the User model that is active in this project.
if you modify(adding new field into it) default User table you need to use get_user_model it will return active User table.
BTW User will return native from django.contrib.auth.models

ModelForm not showing in admin

I have an app called profile which has de model Profile(models.Model). Then, I have another app called offer with the model Offer(models.Model) and it has almost all the attributes of the Profile one, so I wanted to include a form with the attributes of profile in the offer.
In the forms.py in the offer app, I have created:
from django.forms import ModelForm
from profile.models import Profile
from offer.models import Offer
class ProfileOfferForm(ModelForm):
#extrafields
class Meta:
model = Profile
exclude = ('min_salary',)
And then in the admin.py (in the app Offer too) I have:
from django.contrib import admin
from django import forms
from offer.models import *
from offer.forms import *
# Register your models here.
from profile.models import Profile
#admin.register(Offer)
#admin.site.register(Offer, OfferAdmin, ProfileForm)
class OfferAdmin(admin.ModelAdmin):
inlines = [
QuestionInline,
AutomatismInline,
CandidateInline
]
form = ProfileOfferForm
And I get no errors, the only problem is that de fields of Profile don't appear in the administration in the section Offer. Am Ileaving something?
Any help will be appreciated,
Thanks!
It looks like you forget to register your custom Admin to your Model.
# At the bottom do something like this
admin.site.register(Offer, OfferAdmin)

Django Admin tables not displaying correctly

Has anyone seen this before? I've tried making new apps, projects, etc.
All thats in my admin.py file is:
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
admin.site.register(Tribe)
admin.site.register(Membership)
admin.site.register(UserProfile)
I've not got any static files or css in the app..?
Create a class that inherit admin.ModelAdmin, update the fields to be shown in the list_display tuple, and register TribeAdmin instead of Tribe. Do the same for the rest.
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
class TribeAdmin(admin.ModelAdmin):
list_display = ('field_1', 'field_2',)
admin.site.register(Tribe, TribeAdmin)
# admin.site.register(Membership)
# admin.site.register(UserProfile)
For all the available options, have a look at the documentation or an easy to understand beginner tutorial from the DjangoBook (please note its for an outdated Django Version, but fields works with Django 1.8)
With Django 1.8 you can use.
#admin.register(Tribe)
class TribeAdmin(admin.ModelAdmin):
list_display = ('field',)

Django import-export buttons DO NOT appear in admin screen

I followed the django import-export manual but the Import Export buttons do not appear in my admin screen.
This is what I have in my admin.py. Is there anything else I need to do? I have added import-export to my settings.py.
from django.contrib import admin
from costtool import models as m
from costtool.models import UserProfile, Prices
from import_export import resources
from import_export.admin import ImportExportModelAdmin, ImportMixin
class PriceResource(resources.ModelResource):
class Meta:
model = Prices
class PriceAdmin(ImportExportModelAdmin):
resource_class = PriceResource
pass
admin.site.register(UserProfile)
admin.site.register(Prices)
Just tell the admin what ModelAdmin to use:
admin.site.register(Prices, PriceAdmin)
You can check Django's ModelAdmin doc and try to use the new register decorator if you're using Django 1.7