-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Создал модель юзера, настроил ему админку.
- Loading branch information
1 parent
f7f1a5a
commit a860faa
Showing
3 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
# from django.contrib import admin | ||
from django.contrib import admin | ||
|
||
# Register your models here. | ||
from .models import User | ||
|
||
|
||
class UserAdmin(admin.ModelAdmin): | ||
fields = [ | ||
'username', | ||
'is_staff', | ||
'is_active', | ||
'role', | ||
'first_name', | ||
'last_name', | ||
'email', | ||
'date_joined', | ||
'last_login', | ||
'location', | ||
'birth_date', | ||
] | ||
|
||
list_display = [ | ||
'id', | ||
'username', | ||
'is_staff', | ||
'is_active', | ||
'role', | ||
'email', | ||
'date_joined', | ||
'last_login', | ||
] | ||
search_fields = ('role',) | ||
ordering = ('id',) | ||
|
||
|
||
admin.site.register(User, UserAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
# from django.db import models | ||
from django.contrib.auth.models import AbstractUser | ||
from django.db import models | ||
from django.utils import timezone | ||
|
||
# Create your models here. | ||
|
||
class User(AbstractUser): | ||
""" | ||
Расширение встроенной модели User.""" | ||
USER = 'user' | ||
MODERATOR = 'moderator' | ||
ADMIN = 'admin' | ||
|
||
CHOISES = [ | ||
(USER, 'Аутентифицированный пользователь'), | ||
(MODERATOR, 'Модератор'), | ||
(ADMIN, 'Администратор'), | ||
] | ||
|
||
username: str = models.CharField( | ||
'Username', | ||
unique=True, | ||
max_length=150, | ||
) | ||
email: str = models.EmailField( | ||
'E-mail address', | ||
unique=True, | ||
blank=False, | ||
max_length=254, | ||
) | ||
role: str = models.CharField( | ||
max_length=9, | ||
choices=CHOISES, | ||
default='user' | ||
) | ||
first_name: str = models.CharField( | ||
'first name', | ||
max_length=150, | ||
blank=True | ||
) | ||
last_name: str = models.CharField( | ||
'last name', | ||
max_length=150, | ||
blank=True | ||
) | ||
date_joined = models.DateTimeField( | ||
"date joined", | ||
default=timezone.now | ||
) | ||
last_login = models.DateTimeField( | ||
'last_login', | ||
blank=True, | ||
null=True | ||
) | ||
location: str = models.CharField( | ||
'your city', | ||
max_length=30, | ||
blank=True | ||
) | ||
birth_date = models.DateField( | ||
'birth_date', | ||
null=True, | ||
blank=True | ||
) | ||
|
||
class Meta: | ||
ordering = ('id',) | ||
|
||
def __str__(self): | ||
return self.username | ||
|
||
@property | ||
def is_moderator(self): | ||
return self.role == User.MODERATOR | ||
|
||
@property | ||
def is_admin(self): | ||
return self.role == User.ADMIN | ||
|
||
@property | ||
def is_user(self): | ||
return self.role == User.USER |