https://github.com/guifernandess7/django-usermodel-studies
Django user model study
https://github.com/guifernandess7/django-usermodel-studies
django django-orm django-user-model
Last synced: 5 months ago
JSON representation
Django user model study
- Host: GitHub
- URL: https://github.com/guifernandess7/django-usermodel-studies
- Owner: GuiFernandess7
- Created: 2023-04-24T22:21:05.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-06T20:17:24.000Z (over 2 years ago)
- Last Synced: 2025-03-14T22:45:17.232Z (10 months ago)
- Topics: django, django-orm, django-user-model
- Language: Python
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Django UserModel Studies
Django UserModel Studies
### CustomUserModel:
The class "AuthUser" inherits from User default model, and it is possible to filter if the
user is active or not:
To access all users:
```
AuthUser.objects.all()
```
To filter by users:
```
AuthUser.inactive.all()
AuthUser.active.all()
```
To access if a user is active:
```
x = AuthUser.objects.get(id=3)
check_active(x)
```
### ExtendedUserFields:
The class UserProfile has fields that are added to the default user model
and model forms are created and the user form is shown together with the user profile fields
##### Default User fields:
- First name
- Last name
##### Extended user fields:
- Age
- Nickname

### ExtendedUserFields2
The class NewUser inherits from AbstractUser and it allows being able to extend the User Model and create new fields.
##### **Step by Step**:
Step 1 - Change models.py to
```
from django.db import models
from django.contrib.auth.models import AbstractUser
class NewUser(AbstractUser):
age = models.IntegerField(null=True, blank=True)
nickname = models.CharField(max_length=100, null=True, blank=True)
```
Step 2 - Add the following line in settings.py:
```
AUTH_USER_MODEL = 'user.NewUser'
```
Step 3 - Allows the information to display in the User Menu. In admin.py:
```
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import NewUser
class CustomUserAdmin(UserAdmin):
fieldsets = (
*UserAdmin.fieldsets,
(
"Additional Info",
{
'fields': (
'age',
'nickname',
)
}
)
)
admin.site.register(NewUser, CustomUserAdmin)
```
or
```
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import NewUser
fields = list(UserAdmin.fieldsets)
fields[1] = ('Personal Information', {'fields': ('first_name', 'last_name', 'email', 'age', 'nickname')})
UserAdmin.fieldsets = tuple(fields)
admin.site.register(NewUser, UserAdmin)
```
The result:
