[Answered ]-Implementing the charity and Benefactor model in Django

1πŸ‘

βœ…

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'
πŸ‘€Xus

Leave a comment