django - 为什么在同一文件中使用此 django 模型时未定义?

标签 django model

我正在使用 Django 并尝试在其下有相关模型的模型上设置外键。我有一个名为 Candidates Applied 的外键,它与其下的 Job 模型相关。错误是““作业”未定义”,但它就在它的下面。在同一文件中使用模型关系时,Django 是否需要特定的顺序?

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)

提前谢谢您,

最佳答案

定义与之前尚未定义的模型的关系时,请将其括在引号中。所以现在,它将是:

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

此外,Django 模型名称采用单数也是一个好习惯。了解更多 here关于何时加引号和何时不加引号的做法。

关于django - 为什么在同一文件中使用此 django 模型时未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76215672/

相关文章:

django - 无法为 API 设置 'rest_framework_jwt.authentication.JSONWebTokenAuthentication' 导入 'DEFAULT_AUTHENTICATION_CLASSES'

javascript - Django 在客户端上传之前调整图像大小

python - 在后台运行特定的 Django 脚本

ruby-on-rails - 如何将多个 Controller 与一个模型一起使用?

python - 在创建django时添加注册

mysql - CakePHP 添加的列没有出现在查询中

python - 将 Django 模型移动到它们自己的文件中

python - SQLAlchemy:filter_by with like() on foreign attribute

python - 整数超出范围

javascript - 如何强制每个浏览器将表单数据设置为其实际值