I use the admin account login to the /admin/:
Why there are only these field I can edit?
I have write tons models in my project. why do not show up?
Put following code in your django app's admin.py file
from django.apps import apps
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered
app_models = apps.get_app_config('my_app').get_models()
for model in app_models:
try:
admin.site.register(model)
except AlreadyRegistered:
pass
ref : https://docs.djangoproject.com/en/2.0/ref/contrib/admin/ and Register every table/class from an app in the Django admin page
Related
I am using Django 1.10 when I use Django admin but I can not find my table in admin setting, because I just add my table by SQL but not use Django migrate what should I do to add some table in Django admin
I use #admin.register() in admin.py so I can see this in the setting because I am superuser
it shows in admin index
but in User permissions settings it does not show
because it does not in DB table auth_permission and some relative table
empyt
you should add in the file admin.py:
from django.contrib import admin
from .models import name_of_your_model
Register your models here.
admin.site.register(name_of_your_model)
so you can see your tables created from the models in the administrator interface
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 have REST API built on Django and JS application. Both are on different domains. How to change django admin "VIEW SITE" link in such way so it will open JS application? I've tried to pass absolute link (https://docs.djangoproject.com/es/1.10/ref/contrib/admin/#django.contrib.admin.AdminSite.site_url), but looks like it does not work - only relative paths allowed
I prefer resetting admin.site.site_url in urls.py, along with other site changes (instead of doing this in an admin.py file, cause a project can have serveral admin.py)
# urls.py
admin.site.site_url = '' # Removes the 'View Site' link
admin.site.site_header = 'My Site'
In Django 1.11.5, it seems that :
from django.contrib import admin and
admin.site.site_url = 'https:....' in the admin.py file is enough
By default , "VIEW SITE" points to '/' i.e localhost:8000 (Default settings assumed).
To change it , use (in admin.py):
admin.site.site_url = "/mySite"
Tested on Django 2.1
There are two solutions I can come up with.
Firstly, you could use custom template admin/base.html. But, reading through the default template, you would have to copy-paste a lot of code just to change a link, which seems like an overkill.
Another solution involves overriding AdminSite. AdminSite has a property called site_url, and it seems like changing it would do the job. So, in essense, you can do something like this:
your_app/admin.py
from django.contrib.admin import AdminSite
from .models import MyModel
class MyAdminSite(AdminSite):
site_url = 'https://yourdomain.com'
admin_site = MyAdminSite(name='myadmin')
your_project/urls.py
from django.conf.urls import url
from myapp.admin import admin_site
urlpatterns = [
url(r'^myadmin/', admin_site.urls),
]
And you should register all your models with your custom admin, not Django's default:
from your_app.admin import admin_site
admin_site.register(MyModel)
Default link to viewsite is http://127.0.0.1:8000/
We can change it to custom url through register it in admin.py
admin.site.site_url = "/mySite"
Example : admin.py
from django.contrib import admin
from auth_app.models import profile
# Register your models here.
admin.site.register(profile)
#register link
admin.site.site_url = "/mySite"
In admin.py, mention your custom path like following
admin.site.site_url = "/<Your Path>"
If your custom url is like "https://example.com/dashboard", You have to put below line in your admin.py
admin.site.site_url = "/dashboard"
You can change "VIEW SITE" link by changing "admin.site.site_url" which is "/" by default:
from django.contrib import admin
print(admin.site.site_url) # /
admin.site.site_url = "/example"
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.