App label over-riding an earlier app label - django

How can I assign a Django app, a label (e.g. auth) to over-ride an earlier application?
The django.contrib.auth.… modules claim the app label auth. I want to have that installed, but claim the auth label for my site's custom auth app.
INSTALLED_APPS = [
...,
'django.contrib.auth',
...,
'lorem.auth.AppConfig',
...,
]
So, the code (models, migrations, etc.) are in effect because django.contrib.auth is installed; but I want lorem.auth to get precedence for the auth app label.

there is one other way you have to import any one model from auth like permission model in any of your models.py and add something like this
using meta api of django:
from django.contrib.auth.models import Permissions
Permissions._meta.app_config.verbose_name = "new_name"
you can specify the new name like this of your existing auth name and after that you wont get any conflict in your app_name
i think this might work for you

Related

Impossible to use django-allauth when there is already an `account` app?

My Django application already has an app called account. Does it mean that it is ABSOLUTELY impossible to use django all-auth because of the name conflict? Due to the existing data, the app account cannot be renamed.
settings.py:
INSTALLED_APPS = [
...
'account',
...
# For allauth:
'django.contrib.sites',
'allauth',
'allauth.account', # Name conflict
...
If so, is there a good alternative?
2-14
Per solarissmoke's suggestion. Where should I put the new app and what is it name?
Is it something like this (Of course, it is wrong)?
my_project/account/apps.py:
import allauth.account
from django.apps import AppConfig
class AccountConfig(AppConfig):
name = 'account'
class AllAuthAccountConfig(allauth.account):
name = 'allauth.account'
label = 'allauth_account' # Change this
verbose_name = 'aullauth_account'
This is a known problem with django-allauth.
You can work around it by changing your own app to use a different app label. In your app's AppConfig:
class AccountConfig(AppConfig):
name = 'my_project.apps.account'
label = 'my_project_account' # Change this
verbose_name = 'account'
And refer to this app config in your INSTALLED_APPS, e.g.,
INSTALLED_APPS = [
...
'account.apps.AccountConfig',
...
'allauth',
'allauth.account',
...
Which should now work because the app labels are unique. Note that the only issue with this is that database tables names for your account app will have to change so as not to conflict with the allauth app - this will require some data migrations (if on an established project) or creation of fresh migrations (if on a project where you can afford to clobber the database).
You can also do this with the allauth.account app if that's easier - just create a new app config anywhere in your project, e.g.,
my_project/allauth_apps/apps.py (make sure to also create __init__.py in this new directory):
class AllAuthAccountConfig(allauth.account):
name = 'allauth.account'
label = 'allauth_account' # Change this
verbose_name = 'aullauth_account'
And then in your INSTALLED_APPS replace account with my_project.allauth_apps.apps.AllAuthAccountConfig. As above, this changes the database table names.
you need to fork the git on your own and change the label
fork the source code from https://github.com/pennersr/django-allauth/
add unique label such as allauthaccount to app in django-allauth/allauth/account/apps.py on your own forked git
commit
add following line to your requirements.txt -e git+https://github.com/andylee0213/django-allauth#egg=django_allauth
do "pip install -r requirements.txt and pip freeze > requirements.txt" and check github link is still in requirements.txt
but my suggestion is, instead of not receiving updates and going through all this pain, just use other auth libraries. There are many other libraries that does not depend on all auth. check
https://medium.com/codex/django-allauth-vs-dj-rest-auth-vs-python-social-auth-vs-drf-social-oauth2-ef7d50f92d16

Hide the token table from the admin panel in Django REST Framework

I'm using Django REST Framework and Django-OAuth-toolkit to enable OAuth2 authentication in my application.
Since after using OAuth2, I no more need token-based authentication and hence no token table/model.
Sometimes it makes me confused after seeing two different modules for handling token.
Therefore, I want to remove/hide Token table from the admin panel of Django.
Here is my settings.py file
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication'
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
I have removed Token based authentication but still Token table is there in the admin panel
You don't have to remove rest_framework.authtoken, you can just kick out its registered admin.
This answer likely doesn't apply to you but if you want to carry on using authtokens and just make them hidden from the Admin, you can add the following to one of your existing admin.py files:
try:
from rest_framework.authtoken.models import TokenProxy as DRFToken
except ImportError:
from rest_framework.authtoken.models import Token as DRFToken
admin.site.unregister(DRFToken)
Why the ugly code? It's to cope with a 2020 edit whereby the ModelAdmin used here is registered against a proxy model to another which picks the User's PK as its main primary key (for URLs, etc) rather than the database ID of the Token. These are one-to-one mapped, so it makes some sense.
If you know you're only supporting DRF 3.12.0 and newer, you can hack this down to TokenProxy.
from rest_framework.authtoken.models import TokenProxy
admin.site.unregister(TokenProxy)
Get to any registered app's admin.py and add the below lines.
from rest_framework.authtoken.models import TokenProxy
admin.site.unregister(TokenProxy)
Atleast, this works as per 2021 using Django 3.1.7.
You have to remove rest_framework.authtoken from INSTALLED_APPS
See the docs
from rest_framework.authtoken.models import Token
admin.site.unregister(Token)
if you do the above one you will get
"raise NotRegistered('The model %s is not registered' % model.name)
django.contrib.admin.sites.NotRegistered: The model Token is not registered"
So please follow this below approch
from rest_framework.authtoken.models import TokenProxy
admin.site.unregister(TokenProxy)
This should normally work
from rest_framework.authtoken.admin import (
TokenProxy
)
admin.site.unregister(TokenProxy)

"polls.apps.PollsConfig" and "polls" in `INSTALLED APPS'

To tell Django which apps are installed, the official documentation introduces
The advantage 'polls.apps.PollsConfig' over 'polls'
setting.py
INSTALLED_APPS = [
#my APPs
"polls.apps.PollsConfig",
]
This explicitly refer to the installed app in app.py
from django.apps import AppConfig
class LearningLogsConfig(AppConfig):
name = 'learning_logs'
However, in some books, it tell it in a shortcut
setting.py
INSTALLED_APPS = [
#my APPs
"polls",
]
How Django access 'polls' in this situation?
Django can use either option. The version with the AppConfig lets you point Django specifically to an application configuration class which lets you specify some additional config for the app.
If you just want the app as-is, refer to it by its name alone. Use the AppConfig variant only when you are supplying an AppConfig class to configure something about the application.
In the manage.py location Django will check with the string you have given in installed apps list.
Each string should be a dotted Python path to:
an application configuration class (preferred), or
a package containing an application.

Change header 'Django administration' text on nginx

I followed this question's answers to change my django admin panel title header.
I tried this:
There is an easy way to set admin site header - assign it to current
admin instance in urls.py like this
admin.site.site_header = 'My admin'
But it just works when I'm running the page via Python manage.py runserver
My question is how can I change the admin title header when I'm running the site via gunicorn and nginx
writing this code at the bottom of urls.py somehow worked:
admin.site.site_header = 'My admin'
If you already have an admin.py file started wherein you have registered your particular models, you can simply adjust these values there.
your_app/admin.py
# Simple admin setup
from django.contrib import admin
from .models import MyModel
# Register model
admin.site.register(MyModel)
# Tweak admin site settings like title, header, 'View Site' URL, etc
admin.site.site_title = 'My App Admin'
admin.site.site_header = 'My App Admin'
You can find all the attributes here.
follow the below steps to customize the site header and site title text of django admin login page :
1.)First import admin module in settings.py file like as below :
from django.contrib import admin
2.)In bottom of settings.py file add these lines:
admin.site.site_header = 'MY_SITE_HEADER'
admin.site.site_title = 'MY_SITE_TITLE'
The above method works in latest version of django i.e.1.11.3 till date.
You can make changes to parts of the admin by providing a template in an admin subdir of your templates directory to override what is provided by admin.
In this case, you'd want to provide a base_site.html template. You can see what the default one looks like here: https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base_site.html

Django admin App

I am building a app. The app will build a Poll OR a Quiz.
So my models will have a type field(Poll, Quiz)
Now i would like to display the 2 "Types" in the admin App list. But i dont what to create two apps Poll and Quiz. Is there a way, to display the 2 options in the list and then when you click on lets say Poll, the type field is set to Poll and then you fill in the rest of the models fields.
Thanks
have a short look to the second tutorial page of django. It describes the how to do that.
http://docs.djangoproject.com/en/1.1/intro/tutorial02/#intro-tutorial02
You need to activate the admin site:
Add "django.contrib.admin" to your INSTALLED_APPS setting.
Run python manage.py syncdb.
update urls.py
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
add the next line to urlpatterns
(r'^admin/', include(admin.site.urls)),
2 . You need to add your models to the admin interface
You only have to create a admin.py in your application directory (e.g. polls) and fill in the following content:
from mysite.polls.models import Poll, Quiz
from django.contrib import admin
admin.site.register(Poll)
admin.site.register(Quiz)
you have to change the first line of course to fit with your project name.
Hope this will help!
alas!I figured it out!
What you use is a Django Proxy Model
http://docs.djangoproject.com/en/1.1/topics/db/models/#id8
So I set up a Proxy model in my models.py file and then in admin.py I just used the proxy models as Admin.