为什么在同一文件中使用Django模型时它会变成未定义的?

huangapple go评论58阅读模式
英文:

Why is this django model coming undefined when using it in the same file?

问题

I am using Django and trying to set a Foreign Key on a model that has its relating model under it. I have a foreign key called on Candidates Applied that is related to the Job model under it. The error is "Job" is not defined, but its just under it. Does Django need a specific order when using model relations within the same file?

class CandidatesApplied(models.Model):
    job = models.ForeignKey(Job, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    resume = models.CharField(max_length=200)
    appliedAt = models.DateTimeField(auto_now_add=True, null=True)
    coverLetter = models.CharField(max_length=3000, null=True)

    class Meta:
        default_related_name = 'candidates_applied'

class Job(models.Model):
    title = models.CharField(max_length=200, null=True)
    description = RichTextField(blank=True, null=True)
    email = models.EmailField(null=True)
    featured = models.BooleanField(default=False, auto_created=False)
    expiredAt = models.DateField(default=date.today, auto_now=False)
    address = models.CharField(max_length=100, null=True)
    job_country = models.JSONField(null=True)
    job_facebook = models.CharField(max_length=100, null=True)
    job_twitter = models.CharField(max_length=100, null=True)
    job_state = models.JSONField(null=True)
    jobType = ArrayField(models.JSONField())
    education = models.CharField(
        max_length=100,
        choices=Education.choices,
        default=Education.Bachelors
    )
    job_city = models.JSONField(null=True)
    industry = models.CharField(
        max_length=100,
        choices=Industry.choices,
        default=Industry.Business
    )
    experience = models.CharField(
        max_length=100,
        choices=Experience.choices,
        default=Experience.NO_EXPERIENCE
    )
    minSalary = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(1000000)])
    maxSalary = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(1000000)], null=True)
    positions = models.IntegerField(default=1)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
    responsibilities = RichTextField(blank=True, null=True)
    skills = RichTextField(blank=True, null=True)
    lat = models.DecimalField(max_digits=9, decimal_places=6, null=True)
    long = models.DecimalField(max_digits=9, decimal_places=6, null=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    createdAt = models.DateTimeField(auto_now_add=True, null=True)
    startAt = models.DateField(null=True, default=datetime.date.today)
    weeklyHours = models.DecimalField(max_digits=4, decimal_places=2, null=True, default=0.00)
    isActive = models.BooleanField(default=True, auto_created=True)
    appliedApplicants = models.ForeignKey(CandidatesApplied, on_delete=models.CASCADE, null=True)
英文:

I am using Django and trying to set a Foreign Key on a model that has its relating model under it. I have a foreign key called on Candidates Applied that is related to the Job model under it. The error is ""Job" is not defined", but its just under it. Does Django need a specific order when using model relations within the same file?

class CandidatesApplied(models.Model):
    job = models.ForeignKey(Job, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null = True)
    resume = models.CharField(max_length=200)
    appliedAt = models.DateTimeField(auto_now_add=True, null=True)
    coverLetter = models.CharField(max_length=3000, null=True)

    class Meta:
        default_related_name = 'candidates_applied'

class Job(models.Model):
    title = models.CharField(max_length=200, null=True)
    description = RichTextField(blank=True, null=True)
    email = models.EmailField(null=True)
    featured = models.BooleanField(default=False, auto_created=False)
    expiredAt = models.DateField(default=date.today, auto_now=False)
    address = models.CharField(max_length=100, null=True)
    job_country = models.JSONField(null=True)
    job_facebook = models.CharField(max_length=100, null=True)
    job_twitter = models.CharField(max_length=100, null=True)
    job_state = models.JSONField(null=True)
    jobType = ArrayField(models.JSONField())
    education = models.CharField(
        max_length=100,
        choices=Education.choices,
        default=Education.Bachelors
    )
    job_city = models.JSONField(null=True)
    industry = models.CharField(
        max_length=100,
        choices=Industry.choices,
        default=Industry.Business
    )
    experience = models.CharField(
        max_length=100,
        choices=Experience.choices,
        default=Experience.NO_EXPERIENCE
    )
    minSalary = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(1000000)])
    maxSalary = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(1000000)], null=True)
    positions = models.IntegerField(default=1)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
    responsibilities = RichTextField(blank=True, null=True)
    skills = RichTextField(blank=True, null=True)
    lat = models.DecimalField(max_digits=9, decimal_places=6, null=True)
    long = models.DecimalField(max_digits=9, decimal_places=6, null=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    createdAt = models.DateTimeField(auto_now_add=True, null=True)
    startAt = models.DateField(null=True, default=datetime.date.today)
    weeklyHours = models.DecimalField(max_digits=4, decimal_places=2, null=True, default=0.00)
    isActive = models.BooleanField(default=True, auto_created=True)
    appliedApplicants = models.ForeignKey(CandidatesApplied, on_delete=models.CASCADE, null=True)

Thank you in advance,

答案1

得分: 2

当定义与之前未定义的模型的关系时,您需要将其用引号括起来。所以现在,它将是:

class CandidateApplied(models.Model):
    job = models.ForeignKey("Job", on_delete=models.CASCADE)
    ...

另外,对于Django模型名称,最好使用单数形式。有关何时使用引号和何时不使用引号的实践,请阅读更多信息这里

英文:

When defining a relationship to a model that has not been defined prior to that point, you enclose it in quotes. So now, it'll be:

class CandidateApplied(models.Model):
    job = models.ForeignKey("Job", on_delete=models.CASCADE)
    ...

Also, it is a good practice for Django model names to be singular. Read more here about the practices when to put quotes and when not.

huangapple
  • 本文由 发表于 2023年5月10日 14:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定