I want the user to be able to login and logout only, but not edit their profile themselves, only the admin can register and edit the user and their profile.
# my_app/models.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
field1 = models.CharField(max_length=100, blank=True)
field2 = models.CharField(max_length=50, blank=True)
# my_app/admin.py
from django.contrib import admin
from my_app.models import UserProfile
admin.site.register(UserProfile)
After applying the migrations there's still no "field1" and "field2" in the admin dashboard. How come?
#settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = '!&d@p=()6crkiyu=z1$_vsr%2or#v3396@f7n9tvwykd+o9^ol'
TEMPLATE_DEBUG = True
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli_extensions',
'grappelli',
'my_app',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'my_project.urls'
WSGI_APPLICATION = 'my_project.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'some_db_dev',
'USER': 'user1',
'PASSWORD': '.....$',
'HOST': '127.0.0.1'
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
LOGIN_URL = '/login'
The problem is that you have just created a user model called UserProfile
, but have not modified the admin so that it is shown along with the normal (built-in) User
model.
Please see the documentation on extending the User
model for more details - make sure you are viewing the page for the version of django you are using.
To just adjust the profile as a normal model, you need to view its own change form, which should be available from the main admin landing page.