pythondjangodjango-modelsdjango-inheritance

Implementing the charity and Benefactor model in Django


am new to Django and I getting some difficulties in implementing this model in Django:

enter image description here

here's my fiels :

.
├── 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)

But this is not true and it has a problem.

My problem is inheritance and how I can implement such a model. i'm new to Django Models and i have some difficulties in implementing this model in Django.

Thick fields indicate the key (ForeinKey or PrimaryKey or ...) of that field.

The faintness of the fields indicates that the field is optional.

Italic field names indicate that the field is inherited and inherited from the parent class.


Solution

  • It should be sth like this:

    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)
        
        
    

    Also, there are other ways to do so.For example, you could use validators for the phone field.

    Or, Using classes for choices like this one:

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