Skip to content

Commit

Permalink
Создал модель юзера, настроил ему админку.
Browse files Browse the repository at this point in the history
  • Loading branch information
Govorlivyh committed Oct 15, 2023
1 parent f7f1a5a commit a860faa
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/healthy_food/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,5 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'users.User'
36 changes: 34 additions & 2 deletions backend/users/admin.py
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)
82 changes: 80 additions & 2 deletions backend/users/models.py
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

0 comments on commit a860faa

Please sign in to comment.