使用其他模型添加模型约束

时间:2020-05-17 11:03:07

标签: python django django-rest-framework

我目前正在开发具有候选人,应用程序和工作模型的应用程序跟踪应用程序。

理想情况下,我希望候选人只申请一份工作,但是如果他们愿意,候选人应该能够申请另一份工作。

我遇到的问题是,因为应用程序模型位于候选对象和作业之间,我不确定在哪里添加约束-应用程序模型通过以下位置“检测”候选对象是否已存在:应用程序模型中定义的关系。

添加唯一约束将只能使候选人在整个过程中应用一次,所以这不是理想的选择。

  • 候选人A->应用程序A->工作A#应该可以工作
  • 候选人A->应用程序A->作业A#应该抛出错误
  • 候选人A->应用程序B->工作B#应该可以工作

这是我的模特。py


     class Candidate(models.Model):
            # Candidate Personal and Contact Information
            date_created = models.DateTimeField(auto_now_add=True)
            first_name = models.CharField(max_length=120)
            last_name = models.CharField(max_length=120)
            email = models.EmailField()
            phone = models.CharField(max_length=30)

        # Candidate Location Information
        city = models.CharField(max_length=120)
        state = models.CharField(max_length=120)
        country = CountryField(blank_label='Select Country')
        zip_code = models.CharField(max_length=10)

        def __str__(self):
            return f'{self.first_name} {self.last_name}'


class Application(models.Model):

    # Application Status Choices 
    class ApplicationStatus(models.TextChoices):
       ...


    # Application Stages Choices 
    class ApplicationStage(models.TextChoices):
       ...


    # Job-related Information
    date_applied = models.DateTimeField(auto_now_add=True)
    job = models.ForeignKey('Job', on_delete=models.CASCADE)
    application_status = models.CharField(max_length=120,
                                          choices=ApplicationStatus.choices,
                                          default=ApplicationStatus.ACTIVE)
    stage = models.CharField(max_length=120,
                             choices=ApplicationStage.choices,
                             default=ApplicationStage.APPLICATION)

    # Applicant Information
    candidate = models.ForeignKey(Candidate, 
                                  related_name='applications', 
                                  on_delete=models.CASCADE)
    resume = models.FileField()

    def __str__(self):
        return str(self.job)


class Job(models.Model):

    # Job Status Choices
    class JobStatus(models.TextChoices):
        ...


    # Employment Type Choices
    class EmploymentType(models.TextChoices):
        ...

    date_created = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, models.SET_NULL, blank=True, null=True)
    job_status = models.CharField(max_length=120,
                                  choices=JobStatus.choices,
                                  default=JobStatus.DRAFT)
    employment_type = models.CharField(max_length=120,
                                       choices=EmploymentType.choices,
                                       default=EmploymentType.FULL_TIME)
    compensation_min = models.DecimalField(max_digits=6, decimal_places=2)
    compensation_max = models.DecimalField(max_digits=6, decimal_places=2)
    title = models.CharField(max_length=120)
    description = models.TextField()

    def __str__(self):
        return self.title

这是我的serializers.py

from rest_framework import serializers
from rct.models import (Candidate,
                        Application,
                        InterviewSchedule,
                        Scorecard,
                        Job)


class CandidateSerializer(serializers.ModelSerializer):
    applications = serializers.StringRelatedField(many=True, read_only=True)
    class Meta: 
        model = Candidate
        fields = '__all__'


class ApplicationSerializer(serializers.ModelSerializer):
    class Meta: 
        model = Application
        fields = '__all__'


class JobSerializer(serializers.ModelSerializer):

    class Meta: 
        model = Job
        fields = '__all__'

如果有人能指出正确的方向,我将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您可以在job模型 Meta 类的candidateApplication字段上使用unique_together索引。要获取更多信息,请访问官方model options文档。

相关问题