I've tried to override AdminSite class with my own custom class. I followed tutorial from django's documentation: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-adminsite but it didn't work. To be specific, I'd like to override original AdminSite with my own class and not just add another admin site into my project.
I've created my custom class MyAdminSite which inherit from class
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
pass
Then in my app urls.py I've add:
from django.conf.urls import url, include
import django.contrib.admin as admin
from .admin_site import MyAdminSite
admin.site = MyAdminSite()
admin.autodiscover()
urlpatterns = [
url(r'^', admin.site.urls),
]
It seemed to work, but admin models are register to AdminSite insted of MyAdminSite.
I tried three ways of register models to my custom site:
#admin.register(Model)
class ModelAdmin(model.AdminModel):
...
This way models are registered to original AdminSite.
Second way:
#admin.site.register(Model):
class ModelAdmin(model.AdminModel):
...
That don't work and cause exception. The ModelAdmin class isn't passed to register method.
Last way:
class ModelAdmin(model.AdminModel):
...
admin.site.register(Model, ModelAdmin)
That works, but on admin site I can see only my models not models from Django admin (Users and Groups).
How can I permanently override admin.site and register all models to MyAdminSite?
From myapp/admin.py:
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.admin import AdminSite
from django.contrib import admin
from .models import MyModel #This is my app's model
# Custom admin site
class MyAdminSite(AdminSite):
site_header = 'My Project Title'
site_title = 'My Project Title Administration'
index_title = 'My Project Title Administration'
# You can add on more attributes if you need
# Check out https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#adminsite-objects
# Create admin_site object from MyAdminSite
admin_site = MyAdminSite(name='my_project_admin')
# Register the models
class MyModelAdmin(admin.ModelAdmin):
list_display = ('id', 'description')
admin_site.register(MyModel, MyModelAdmin)
# Create and register all of your models
# ....
# This is the default Django Contrib Admin user / group object
# Add this if you need to edit the users / groups in your custom admin
admin_site.register(Group, GroupAdmin)
admin_site.register(User, UserAdmin)
From myproject/urls.py
from django.conf.urls import url
from django.contrib import admin
from myapp.admin import admin_site ##! Important..Import your object (admin_site) instead of your class (MyAdminSite)
urlpatterns = [
url(r'^admin/', admin_site.urls), #Now all /admin/ will go to our custom admin
]
I didn't found the solution to my problem, but I have made a workaround.
First we need to create module in our app (e.g. admin.py) and then extend class AdminSite:
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
...
Then on bottom of module we need to create instance of our MyAdminSite and register built-in models from Django:
site = MyAdminSite()
site.register(Group, GroupAdmin)
site.register(User, UserAdmin)
Necessary imports:
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
In our site url module we need to override original site object:
from .admin import site
admin.site = site
admin.autodiscover()
...
url(r'^admin/', admin.site.urls)
...
Last change we need to do is register our models. One thing we need to remeber is that we can't use register as decorator like that:
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
...
or:
#admin.site.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
...
We need to define our ModelAdmin class and then call register on our MyAdminSite object:
class MyModelAdmin(admin.ModelAdmin):
...
admin.site.register(MyModel, MyModelAdmin)
This is the only solution that is working for me.
I faced a similar problem. I used Django 2.1 and the hook from the comments above didn't work for me. And also I was not able to import GroupAdmin and UserAdmin, like so
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
Importing GroupAdmin or UserAdmin broke the code for some reason. I was not able to define the exact reason.
So my workaround was (in project/urls.py):
from django.conf.urls import include, url
from django.contrib.admin import site
from project.admin import myadmin
myadmin._registry.update(site._registry)
urlpatterns = [
url(r'^admin/', myadmin.urls),
]
The idea here is to copy registered models from default admin site. Maybe it's not good to do so, but I could not find anything else working.
Related
I've setup a Django REST framework project but the api root hasn't been populated with anything eg a users ViewSet and I can't access the expected url with a list of users.
There is one app users, with a custom user model. (and the django project is named api)
main urls.py
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import DefaultRouter
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('users.urls')),
]
the users app urls.py
from django.contrib import admin
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from users.views import CustomUserViewSet
router = DefaultRouter()
router.register("users", CustomUserViewSet, 'users')
urlpatterns = [
]
urlpatterns += router.urls
users models.py
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
def __str__(self):
return self.username
users serialisers.py
from rest_framework.serializers import ModelSerializer
from .models import CustomUser
class CustomUserSerializer(ModelSerializer):
class Meta:
model = CustomUser
fields = '__all__'
users app views.py
from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from .serializers import CustomUserSerializer
from .models import CustomUser
class CustomUserViewSet(ViewSet):
serializer_class = CustomUserSerializer
queryset = CustomUser.objects.all(
And empty api root at localhost:8000/api/
and 404 error at localhost:8000/api/users/
If you are using just Viewset, you need to implement the actions.
From the docs:
The ViewSet class does not provide any implementations of actions. In
order to use a ViewSet class you'll override the class and define the
action implementations explicitly
You can add a list action like in the example:
from rest_framework.response import Response
class CustomUserViewSet(ViewSet):
serializer_class = CustomUserSerializer
queryset = CustomUser.objects.all()
def list(self, request):
queryset = CustomUser.objects.all()
serializer = CustomUserSerializer(queryset, many=True)
return Response(serializer.data)
Or (maybe what you are looking for) using something like ModelViewSet that already includes implementations for various actions:
class CustomUserViewSet(viewsets.ModelViewSet):
serializer_class = CustomUserSerializer
queryset = CustomUser.objects.all()
In your case, you have to simply replace
class CustomUserViewSet(ViewSet):
with
class CustomUserViewSet(viewsets.ModelViewSet):
I'm trying to set verbose_name for a model SocialAuthUser from django_social.
I've tried to use proxy model, setting its Meta.verbose_name to desired value, but had no success (probably I did it wrong). If it's the way to go, I can provide more details.
It would be great to avoid installing module from pip in editable mode just to replace verbose_name in admin site.
Probably I can replace model name in admin site in some other way?
I thought about adding custom link to admin site, but didn't research this method yet because it feels hacky.
You almost got it right. For your changes on the proxy model to take effect you need to unregister the model from admin site first and then register the proxy model.
The example below is for social_django.Association model.
# admin.py
from django.contrib import admin
from social_django.admin import AssociationOption
from social_django.models import Association
class AssociationProxy(Association):
class Meta:
proxy = True
verbose_name = 'custom model'
app_label = 'social_django'
admin.site.unregister(Association)
admin.site.register(AssociationProxy, AssociationOption)
This assumes you are using the default admin site
# urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
When I am developing I constantly need to access data from the admin panel but I do not wish to add all models in admin.py since I do not want them to be accessed in production.
Is there a way to show all models in the admin panel in the development environment and hide (part of) them in production automatically?
I think that would be as simple as this:
# my_app/admin.py
from django.contrib import admin
from django.conf import settings
from .models import MyModel, AnotherModel
class MyModelAdmin(admin.ModelAdmin):
pass
class AnotherModelAdmin(admin.ModelAdmin):
pass
# conditional registration of models
if settings.DEBUG:
admin.register(MyModel, MyModelAdmin)
admin.register(AnotherModel, AnotherModelAdmin)
I have a blog made with Django where I write posts in markdown. I would like to add a view in the bottom of the admin page for each instance of the class Entry (my blog post class) such that I can get a preview of what the markdown looks like, while I'm writing. Just as you get a preview here on Stack Overflow when you create a new post.
I already have an admin class extending ModelAdmin:
class EntryAdmin(admin.ModelAdmin):
list_display = ('title','created')
prepopulated_fields = {'slug': ('title',)}
Is it possible to modify ModelAdmin further, such that it loads a certain html file (blogpost.html) and shows it in the bottom of the admin page?
I made a picture to show exactly what I mean:
NB: I know there are various tools such as Django admin plus, that allows one to add views to the admin interface, but not for each instance of an object.
You can use markdownx for that:
pip install django-markdownx
project settings.py
INSTALLED_APPS =
#. . . .
'markdownx',
]
project urls.py
urlpatterns = [
#[...]
url(r'^markdownx/', include('markdownx.urls')),
]
and then collect static files.
python3 manage.py collectstatic
your models.py
from markdownx.models import MarkdownxField
class MyModel(models.Model):
myfield = MarkdownxField()
your app admin.py
from django.contrib import admin
from markdownx.admin import MarkdownxModelAdmin
from .models import MyModel
admin.site.register(MyModel, MarkdownxModelAdmin)
This should work.
I try to create Django Build-in API Documentation however I can't add objects to the list on the left. When I open localhost:8000/docs/ I don't see list of my objects as at the top of the official Django REST documentation (in this case snippets and users). Currently I am trying in this way:
urls.py:
from rest_framework.documentation import include_docs_urls
API_TITLE = 'API title'
API_DESCRIPTION = '..df.'
urlpatterns = [
url(r'^docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION)),
]
models.py:
class Object1(models.Model):
user = models.ForeignKey(User)
value = models.FloatField()
admin.py based on developer.mozilla.org:
from django.contrib import admin
from .models import Object1
# Register your models here.
admin.site.register(Object1)
I also based on https://docs.djangoproject.com and I tried with this admin.py version:
class Object1Admin(admin.ModelAdmin):
pass
admin.site.register(Object1, Object1Admin)
I was using also other versions but I did not get a positive result with them, too. Any suggestions?
UPDATE
I was trying to create ViewSet in my views file and import that in url to define router as Raja Simon suggested:
views.py:
from rest_framework import viewset
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
urls.py:
from rest_framework.routers import DefaultRouter
# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^', include(router.urls))
]
However I didn't achieve what I wanted to. At this moment my list on Django site looks in this way:
But I would like to have list of my objects like in this case:
Change this
user = models.ForeignKey(User)
To
user = models.ForeignKey('auth.User')
It should probably work.
Just create ViewSet in your views file and import that in url to define router
views.py
from rest_framework import viewset
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
urls.py
# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'users', views.UserViewSet)
url(r'^', include(router.urls)),