python - 在 django 中通过 OnetoOne 键访问数据

标签 python mysql django

我有两个模型使用 OnetoOne 模型 key 连接。

class Job_expectation(models.Model):
    SLAB_CHOICES = (
        ('H', 'Hourly'),
        ('W', 'Weekly'),
        ('M', 'Monthly'),
    )
    EMPLOYER_CHOICES = (
        ('I', 'Individual'),
        ('C', 'Corporate'),
    )
    TYPE_CHOICES=(
        ('P','Part time'),
        ('F','Full time'),
        ('I','Internship'),
        ('L','Freelancing'),
        )
    readytoworkas           =   models.OneToOneField(Readytoworkas)
    salary_slab             =   models.CharField(max_length=1,choices=SLAB_CHOICES,default='M')
    employer_type           =   models.CharField(max_length=1,choices=EMPLOYER_CHOICES,default='C')
    industry                =   models.ForeignKey(organization_sector)
    state                   =   models.ForeignKey(State, related_name="Preferred State of Work")
    city                    =   models.ForeignKey(City, related_name="Preferred City of Work")
    region                  =   models.ForeignKey(Region, related_name="Preferred Region of Work")
    shift_type              =   models.ManyToManyField(Slot_preference)
    job_type                =   models.CharField(max_length=1,choices=TYPE_CHOICES,default='F')
    min_salary              =   models.IntegerField(verbose_name="Minimum expected Salary")
    max_salary              =   models.IntegerField(verbose_name="Maximum expected Salary")
    negotiable              =   models.BooleanField(verbose_name="Is your salary negotiable?")`

class Readytoworkas (models.Model):
    employee_id     = models.ForeignKey(Employee,on_delete=models.DO_NOTHING)
    readytoworkas   = models.ForeignKey(Jobs)`

在 Readytoworkas 模型上进行查询的结果是一个列表。对于列表中的每个元素,我想访问 Job_expectation 表中的相应条目。

怎么做?这就是我现在正在尝试做的事情。

skilllist   = Readytoworkas.objects.filter(employee_id=employee)
      for i in skilllist:
        job_expect        =   Job_expectation.objects.get(readytoworkas=i)
        i.employer_type   =   job_expect.employer_type
        i.job_type        =   job_expect.job_type
        i.shift_type      =   job_expect.shift_type
        i.min_salary      =   job_expect.min_salary
        i.max_salary      =   job_expect.max_salary
        i.state           =   job_expect.state
        i.city            =   job_expect.city
        i.region          =   job_expect.region`

最佳答案

您可以更简单地使用“related_name”:

class Job_expectation(models.Model):
    readytoworkas  = models.OneToOneField(Readytoworkas, related_name='job_expectation')

skilllist   = Readytoworkas.objects.filter(employee_id=employee)
for i in skilllist:
    job_expect = i.job_expectation

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

关于python - 在 django 中通过 OnetoOne 键访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074700/

相关文章:

python - Django 表单发布

python - Matplotlib - 强制 3D 图使用可用的 figsize

Python 错误 - 无法重定向设置的 url

MySQL 加权平均值

django - 减少 django 上的用户 CPU 时间

python - 无法安装mysqlclient

python - Python中的矩阵转置

python - 触发和更新停靠窗口

c# - 创建 app.config 文件

mysql - 随着长度的变化,删除十进制值中的尾随零