我已經遷移了現有的數據庫,我想使用django。我有以下型號:不想在django模型上進行內部連接
class Device(models.Model):
class Meta:
db_table = u'DEVICES'
managed=False
id = models.CharField(primary_key=True, max_length=22, db_column='DEVICE_ID')
name = models.CharField(max_length=40, db_column='DEVICE_NAME')
status = models.CharField(max_length=10, db_column='STATUS')
class DevicePort(models.Model):
class Meta:
db_table = u'DEVICE_PORT'
managed=False
id = models.CharField(primary_key=True, max_length=22, db_column='DEVICE_PORT_ID')
device = models.OneToOneField(Device, db_column='DEVICE_ID')
type = models.CharField(max_length=22, db_column='PORT_TYPE_ID')
port_num = models.CharField(max_length=30, db_column='DEVICE_PORT_NUM')
class IP(models.Model):
class Meta:
db_table = u'IP_NODE'
managed=False
ip_address = models.CharField(primary_key=True, max_length=15, db_column='IP_NODE_NO')
hostname = models.CharField(max_length=40, db_column='IP_HOST')
port = models.OneToOneField(DevicePort, db_column='DEVICE_PORT_ID')
status = models.CharField(max_length=50, db_column='IP_NODE_STATUS')
我想要的Devices
名單和他們的關係IP.ip_address
和IP.hostname
秒。在SQL中,我會做類似於:
SELECT UNIQUE
d.device_name device
FROM
IP_NODE c,
DEVICES d,
DEVICE_PORT e
WHERE
c.ip_node_no = b.ip_node_no
AND c.device_port_id = e.device_port_id
AND e.device_id = d.device_id
AND d.device_name LIKE 'something';
我該如何使用我的django模型?
好的,請逗我。 *爲什麼*你不想讓它使用'INNER JOIN'? –
您可以始終執行您自己的SQL語句,而不是讓ORM指定使用的語句:https://docs.djangoproject.com/en/1.3/topics/db/sql/ – Brandon