I have extended the User model, but now the new user model is in an app called "account" which gives all models inside this app the app label "account". The Django model "Groups" still has the app label "Auth", so now models which all has something to do with auth is in separate apps in the admin site. Is it possibly to change the app label for "Groups"?
Try this:
from django.db.models.loading import get_models
get_models(django.contrib.auth.models)[1]._meta.app_label = 'group' #or whatever
If you need still more flexibility in the admin you could try django-admin-tools. It makes it easy to reorder and group models in different layouts (tabs, collapsible boxes, etc.) and also add dashboard-like features.
Just in case anyone needs this in Django 3.0+:
from django.apps import apps
apps.get_model('auth', 'Group')._meta.app_label = 'group' #or whatever, but have to be a registered app
Please not that this will mess with django internal model handling, e.g. generate migrations in contrib.auth sitepackage and so on
Related
I am having trouble finding where this admin file exists so I can add an extra field. I think it's auto-magically created upon setup.
I want to add a date field, specifically, to the listing page (shown below), perhaps after the UID field so I can know when the user auth was created.
screenshot of django user social auths listing page
Okay here's what I've tried using Django-allauth and I think it somehow works the same with django-socialauth. Just get the gist of the idea and work it to your code
Extend first the SocialAccountAdmin in any of your admin.py files, better if in a specific app like "user", "home", or whatever you prefer.
admin.py
from allauth.socialaccount.admin import SocialAccountAdmin
from allauth.socialaccount.models import SocialAccount
class MySocialAccount(SocialAccountAdmin):
list_display = ('user', 'uid', 'provider', 'date_joined') # I haven't tried just adding a certain list to the list_display, for the meantime add all necessary fields just like how socialauth did
admin.site.unregister(SocialAccount) # Need to unregister the default socialaccount admin
admin.site.register(SocialAccount, MySocialAccount) # Then register it back with the custom made admin
There may be perhaps a better way to do this but this did the work.
Can it be interesting to just add a field to your model ? Adding a DateField for your creation date. Probably you need to understand learn more with : https://docs.djangoproject.com/en/3.0/ref/models/fields/
I have a moderately complicated Django project with a variety of different apps (app1, app2 etc), each with their own models. I am building a MetaApp app to track info about each app, with a MetaApp class in models.py, and fields such as appname and modelname
MetaApp drives an index view that summarizes various aspects of each project. I would like to include a count of the database records for each app. This means that I need to programmatically access models from other apps. If I I know the appname and modelname, how do I programmatically access these models?
projects = MetaApp.objects.all()
projects[0].appname[0].modelname.ojects.all()
This code results in an attribute error, because I am storing the appname and modelname as unicode strings. What is the workaround?
Use the django.db.models.loading.get_model() function:
from django.db.models.loading import get_model
model = get_model(app_name, model_name)
object_list = model.objects.all()
How to hide some of the models?
I need them registred to be used by other models, while calling via FK or nested etc.
I found one solution which is rather much about view layer of (MVC)
http://blog.jholster.com/post/1534211028/hide-app-names-in-django-admin
I would like to set it in admin.py, that some of registred models are hidden.
If the models are in your application, just don't register them in the first place. If the model is in a third party app like the django.contrib.auth then use AdminSite unregister method. You can put this in any admin.py or urls.py important is to be discovered by the admin.autodiscover.
# admin.py
from django.contrib.auth.models import User
admin.site.unregister(User)
I'd like to open up the django admin of a new project I'm working on to our users as well as our staff. The problem I'm having is that users and admins have different ways of using the software, and having the same interface be shared between the two groups would lead to inefficiencies.
Is it possible to have the django admin use a specific template if the user is or isn't staff? Or would I need to create two different admin sites?
You need to create different instances of django.contrib.admin.sites.AdminSite
Docs
Example usage,
# admin.py
from django.contrib.admin.sites import AdminSite
new_admin = AdminSite(name='My New Admin Panel')
new_admin.index_template = "new_admin/index.html"
# urls.py
from myproject.admin import new_admin
(r'^new_admin/(*.)', new_admin.urls)
How to unregister django-evolution from admin?
admin.site.unregister( something )
Yes admin.site has also an unregister() method, you need to pass it the model class of the admin you want to unregister!
from django_evolution.models import Version, Evolution
admin.site.unregister(Version)
admin.site.unregister(Evolution)
Put it in any module that is loaded on startup after the evolution app, for example in admin.py of an app that comes in INSTALLED_APPS AFTER evolution!