【发布时间】:2020-02-26 10:49:03
【问题描述】:
我有两节课;设备和连接。我正在尝试在它们之间建立联系。
设备类;
class Devices(models.Model):
device_no = models.IntegerField(primary_key=True)
device_name= models.CharField(max_length=512)
连接类;
class Connections(models.Model):
connection_id = models.IntegerField(primary_key=True)
device_name1 = models.CharField(max_length=512)
device_name2 = models.CharField(max_length=512)
device_no1= models.ForeignKey(Devices, on_delete=models.CASCADE, db_column="device_no1", related_name="dev1_no")
device_no2= models.ForeignKey(Devices, on_delete=models.CASCADE, db_column="device_no2", related_name="dev2_no")
出于安全考虑,我已更改变量名称
我的目标是在设备一到设备二之间建立连接。这两者具有相同的规格。当我这样使用它时,它不会返回任何错误。但是当我尝试进行查询时,即:
Connections.objects.filter(device1_no=dev_no)
它不返回 Device 类的任何成员。它只返回 Connections 类成员。我也试过了;
Connections.objects.filter(device_no1__device_no=12)
有什么建议吗?
【问题讨论】:
标签: django django-models orm