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
Related
I'm fairly new to Django.
How do I prevent staff users from logging into the Django admin panel? I still want them to be identified as staff, but do not want them to be able to log into the admin panel.
I am using the default Django admin panel.
Thanks
You can limit admin access to only superusers by overriding the default admin site and overriding the has_permission method in your custom admin site to only return True for superusers
myproject/admin.py
from django.contrib import admin
class MyAdminSite(admin.AdminSite):
def has_permission(request):
return request.user.is_active and request.user.is_superuser
myproject/apps.py
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'myproject.admin.MyAdminSite'
myproject/settings.py
INSTALLED_APPS = [
...
'myproject.apps.MyAdminConfig', # replaces 'django.contrib.admin'
...
]
I have added an app to my existing django site and to view it, I created an extra permission overview.view. But, I do not see it in my admin page, so I can also not assign this permission to any user. I think I have all files setup correctly, but I guess I am missing something. I have all the same files in the overview folder as I have in other working folders.
I do see the page, but somehow I am not logged in either.
This is my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
models.py:
from django.db import models
class Overview(models.Model):
class Meta:
permissions = (
('view', "May view the overview"),
)
and (part of) settings.py
INSTALLED_APPS = [
'overview.apps.OverviewConfig'
]
Permissions are stored in the database and so when you add or remove them via the Meta for the model the same is not of course automatically reflected in the database. Permissions are added post the migrations via a post_migrate signal connected from the auth apps appconfig's ready method. See the source code [GitHub]:
post_migrate.connect(
create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions"
)
Hence when one makes changes to the permissions one needs to run makemigrations and migrate to make sure they are added to the database.
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 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
I'm using django_facebook[1] but when I use FacebookCustomUser and run ./manage.py syncdb it doens't generate admin user. How can I proceed?
AUTH_USER_MODEL = 'django_facebook.FacebookCustomUser'
[1]https://github.com/tschellenbach/Django-facebook/