-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
36 lines (27 loc) · 1.03 KB
/
models.py
File metadata and controls
36 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
"""Custom User model with role-based access"""
ROLE_CHOICES = (
('user', 'User'),
('admin', 'Admin'),
)
email = models.EmailField(unique=True)
role = models.CharField(max_length=10, choices=ROLE_CHOICES, default='user')
phone = models.CharField(max_length=15, blank=True, null=True)
address = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name']
class Meta:
db_table = 'users'
ordering = ['-created_at']
def __str__(self):
return self.email
@property
def is_admin(self):
return self.role == 'admin'
@property
def full_name(self):
return f"{self.first_name} {self.last_name}".strip()