在 Django 中实现慈善和 Benefactor 模型

Implementing the charity and Benefactor model in Django

我是 Django 的新手,在 Django 中实现这个模型时遇到了一些困难:

这是我的字段:

.
├── accounts
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   └── views.py
├── charities
│   ├── admin.py
│   ├── apps.py
│   ├── models.py

charities/model.py:

from django.db import models
from accounts.models import User

class Benefactor(models.Model):
    ex_status = [
    (0,'beginner'),
    (1,'middle'),
    (2,'expert'),
    ]
    #id = models.BigAutoField(primary_key = True)
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    exprience = models.SmallIntegerField(choices = ex_status, default = 0)
    free_time_per_week = models.PositiveSmallIntegerField(default = 0)

class Charity(models.Model):
    #id = models.BigAutoField(primary_key = True)
    user = models.OneToOneField(User,on_delete = models.CASCADE)
    name = models.CharField(max_length = 50)
    reg_number = models.CharField(max_length = 10)

class Task(models.Model):
    state_status = [ 
    ("P" , "pending"),
    ("W", "Waiting"),
    ("A", "Assigned"),
    ("D", "Done")
    ]

    gender_limit_status = [
        ("M","Male"),
        ("F","Female"),
    ]
    id = models.BigAutoField(primary_key = True)
    assigned_bonefactor = models.ForeignKey(Benefactor, on_delete=models.CASCADE)
    charity = models.ForeignKey(Charity, on_delete=models.CASCADE)
    age_limit_from = models.IntegerField(blank=True,null=True,)
    age_limit_to = models.IntegerField(blank=True,null=True,)
    date = models.DateField(blank=True,null=True,)
    description = models.TextField(blank=True,null=True,)
    gender_limit = models.CharField(choices = gender_limit_status,max_length = 1)
    state = models.CharField(max_length = 1,choices = state_status, default= 'P')
    title = models.CharField(max_length = 100)

accounts/models.py :

from django.db import models
from django.contrib.auth.models import AbstractUser
import datetime

class User(AbstractUser):
    gender_status = [
        ('M','Male'),
        ('F','Female'),
    ]
    id = models.BigAutoField(primary_key=True)
    address = models.TextField(blank=True,null=True)
    age = models.PositiveSmallIntegerField(blank=True,null=True)
    date_joined = models.DateField(default=datetime.date.today)
    description = models.TextField(blank=True,null=True)
    email = models.EmailField(blank=True,null=True)
    first_name = models.CharField(max_length = 50,blank=True,null=True)
    gender = models.CharField(max_length = 1,choices = gender_status,blank=True,null=True)
    is_active = models.BooleanField(default = True)
    is_staff = models.BooleanField(default = False)
    is_superuser = models.BooleanField(default = False)
    last_login = models.DateTimeField(blank=True,null=True)
    last_name = models.CharField(max_length = 50, blank=True,null=True)
    password = models.CharField(max_length = 24)
    phone = models.CharField(max_length = 15, blank=True,null=True)
    username = models.CharField(unique = True, max_length = 15)

但这不是真的,它有问题。

我的问题是继承以及如何实现这样的模型。 我是 Django 模型的新手,在 Django 中实现这个模型时遇到了一些困难。

粗字段表示该字段的键(ForeinKey 或 PrimaryKey 或...)。

字段模糊表明该字段是可选的。

斜体字段名表示该字段继承自父级class。

应该是这样的:

Accounts.models

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):

    gen_choice = (('F', 'Femail'), ('M', 'Male'))

    address = models.TextField(blank=True, null=True)
    age = models.PositiveSmallIntegerField(blank=True, null=True)
    date_joined = models.DateTimeField(null=True, blank=True)
    description = models.TextField(blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    first_name = models.CharField(max_length=50, blank=True, null=True)
    gender = models.CharField(max_length=50, choices=gen_choice, blank=True, null=True)
    is_active = models.BooleanField(null=True, blank=True)
    is_staff = models.BooleanField(null=True, blank=True)
    is_superuser = models.BooleanField(null=True, blank=True)
    last_login = models.DateTimeField(blank=True, null=True)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    password = models.CharField(max_length=50)
    phone = models.CharField(max_length=15, blank=True, null=True)
    username = models.CharField(max_length=50, unique=True)

Charities.models

from django.db import models
from accounts.models import User

class Benefactor(models.Model):

    Level = ((0, 'Beginner'), (1, 'Intermediate'), (2, 'Expert'))

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    experience = models.SmallIntegerField(choices=Level, default=0)
    free_time_per_week = models.PositiveSmallIntegerField(default=0)


class Charity(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    reg_number = models.CharField(max_length=10)


class Task(models.Model):

    gen_choice = (('F', 'Femail'), ('M', 'Male'))
    sta_choice = (('P', 'Pending'), ('W', 'Waiting'), ('A', 'Assigned'), ('D', 'Done '))

    # id = models.AutoField() !REMOVE id FIELDS 
    assigned_benefactor = models.ForeignKey(Benefactor, null=True, on_delete=models.SET_NULL)
    charity = models.ForeignKey(Charity, on_delete=models.CASCADE, related_name='User_Charity')
    age_limit_from = models.IntegerField(blank=True, null=True)
    age_limit_to = models.IntegerField(blank=True, null=True)
    date = models.DateField(blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    gender_limit = models.CharField(max_length=50, blank=True, null=True)
    state = models.CharField(max_length=50, choices=sta_choice, default='Pending')
    title = models.CharField(max_length=100)
    
    

此外,还有其他方法可以做到 so.For 例如,您可以对 phone 字段使用验证器。

或者,使用 类 进行这样的选择:

class Gender_Choices(models.TextChoices):
        MALE = 'M', 'Male'
        FEMALE = 'F', 'Female'
        UNSET = 'MF', 'Unset'