如何在Django模型中使用两个键进行查询

时间:2019-08-22 02:10:40

标签: python django django-models django-tables2

我在模型中有下表

  1. 以ACCOUNT_ID作为主键的ACCOUNT_MSTR

  2. EXPERIMENT_MSTR,ID为AS主键,对ACCOUNT_MSTR的外键ACCOUNT_ID引用,以及对TEST_MSTR的另一个外键TEST_ID

class Experiments_Mstr(models.Model):
    ID = models.AutoField(primary_key=True)
    EXPERIMENT_ID = models.IntegerField()
    ACCOUNT_ID = models.ForeignKey(to='login.Account_Mstr', to_field='ACCOUNT_ID', on_delete=models.CASCADE)
    USER_ID = models.ForeignKey(to='login.User_Mstr', to_field='USER_ID', on_delete=models.CASCADE)
    TEST_ID = models.ForeignKey(to='ml.Test_Mstr', to_field='ID', on_delete=models.CASCADE)
    STATUS = models.CharField(max_length=10, default='ACTIVE', blank=False)
    TOTAL_TIME = models.IntegerField(default=700, blank=False)
    FIRST_DROP = models.IntegerField(default=310, blank=False)
    SECOND_DROP = models.IntegerField(default=510, blank=False)
    DATAFILE_PATH = models.CharField(max_length=256, default='/tmp', blank=False)
    CREATED_AT = models.DateTimeField(auto_now=True, blank=False)
    CREATED_BY = models.CharField(max_length=10, default='SYSTEM', blank=False)
    MODIFIED_AT = models.DateTimeField(auto_now=True, blank=False)
    MODIFIED_BY = models.CharField(max_length=10, default='SYSTEM', blank=False)
  1. 以ID作为主键的测试MSTR,是对ACCOUNT_MSTR的外键ACCOUNT_ID引用
class Test_Mstr(models.Model):
    ID = models.AutoField(primary_key=True)
    ACCOUNT_ID = models.ForeignKey(to='login.Account_Mstr', to_field='ACCOUNT_ID', on_delete=models.CASCADE)
    TEST_NAME = models.CharField(max_length=256, null=False, blank=False)
    TEST_SHORT_NAME = models.CharField(max_length=10, null=False, blank=False)
    TEST_DESCRIPTION = models.TextField()
    CREATED_AT = models.DateTimeField(auto_now=True, blank=False)
    CREATED_BY = models.CharField(max_length=10, default='SYSTEM', blank=False)
    MODIFIED_AT = models.DateTimeField(auto_now=True, blank=False)
    MODIFIED_BY = models.CharField(max_length=10, default='SYSTEM', blank=False)

还有一个表:

class ExperimentsTable(tables.Table):
    EXPERIMENT_ID = tables.LinkColumn('experimentDetails', args=[tables.A('EXPERIMENT_ID')])
    TEST_ID = tables.Column(accessor='TEST_ID.TEST_SHORT_NAME')
    SELECTION = tables.CheckBoxColumn(verbose_name=('SELECTION'), accessor='ID',
                                     attrs={ "th__input": {"onclick": "toggle(this)"}})
    class Meta:
        model = models.Experiments_Mstr
        exclude = ('ID', 'ACCOUNT_ID', 'USER_ID', 'DATAFILE_PATH', 'TOTAL_TIME', 'FIRST_DROP', 'SECOND_DROP', 'STATUS', 'MODIFIED_AT', 'MODIFIED_BY',)
        attrs = {'class': 'table'}

我现在正在执行以下代码

experiment = getExperimentData(account=account, experimentNo=EXPERIMENT_ID)

我期望在HTML的“测试ID”字段中获得“测试简称”。但是,我得到的实际结果与测试ID无关。

有没有人和我有类似的问题?

0 个答案:

没有答案