NavaneethaKrishnan
NavaneethaKrishnan

Reputation: 1326

Need Contrains for Foreign Key

I am creating a College Management App in Django.

Here is my model. file: accounts/model.py

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

class CustomUser(AbstractUser):
    ROLE = {('student', 'student'),
            ('staff', 'staff'),
            ('account_manager', 'account_manager'),
            ('Admin', 'Admin')}
    role = models.CharField(choices=ROLE, default='student',
                            max_length=20, blank=True, null=True)

I am using the inbuilt user class for all the users (staff, student, HOD and principal). We can identify the user by the role.

No, I want to create a course database, where the staff_id will be the foreign key of CustomUser table. Is there any way to select the user with the role of the foreign key?

class Course(models.Model):
    course = models.CharField(max_length=150)
    start_date = models.DateField()
    end_date = models.DateField()
    instructor = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, related_name='instructor_name')
    examinar = models.ForeignKey(
        CustomUser, on_delete=models.CASCADE, related_name='examinar_name')

    def __str__(self):
        return f'{self.course.name} Batch No: {self.batch_no}'

Here both referring to the same CustomUser foreign key. That's why I have added the related name. (is this the right approach?)

But on the admin page, if I want to add a new course, I am getting all the users. Like this:

enter image description here]1

I want to display the users only if the role is staff. Is it possible?

Upvotes: 1

Views: 39

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477616

Yes, you can filter this with the limit_choices_to=… parameter [Django-doc]:

class Course(models.Model):
    course = models.CharField(max_length=150)
    start_date = models.DateField()
    end_date = models.DateField()
    instructor = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        related_name='instructor_name',
        limit_choices_to={'role': 'staff'}
    )
    examinar = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        related_name='examinar_name',
        limit_choices_to={'role': 'student'}
    )

The related_name=… parameter [Django-doc] is however the name of the relation in reverse. So it is a way to access all Course objects that have as instructor/examinar the user. You thus might want to rename the fields to:

class Course(models.Model):
    course = models.CharField(max_length=150)
    start_date = models.DateField()
    end_date = models.DateField()
    instructor = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        related_name='taught_courses',
        limit_choices_to={'role': 'staff'}
    )
    examinar = models.ForeignKey(
        CustomUser,
        on_delete=models.CASCADE,
        related_name='followed_courses',
        limit_choices_to={'role': 'student'}
    )

Upvotes: 2

Related Questions