Django与遗留数据库

Django与遗留数据库

本文介绍了Django与遗留数据库 - 如何使用DB序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出使用此SQL查询创建的数据库表:

  CREATE TABLE Bill 

时间DATE NOT NULL,
地址VARCHAR2(60)NOT NULL,
ID NUMBER NOT NULL
);

ALTER TABLE Bill ADD CONSTRAINT Bill_PK PRIMARY KEY(ID);

CREATE SEQUENCE Bill_ID_SEQ START WITH 1 NOCACHE ORDER;
CREATE OR REPLACE TRIGGER Bill_ID_TRG BEFORE
INSERT ON Paragony FOR EACH ROW BEGIN:NEW.ID:= Bill_ID_SEQ.NEXTVAL;
END;

我必须使用Django ORM,所以我已经运行 inspectdb 命令。这是自动生成的代码:

 类Bill(models.Model):
time = models.DateField()
address = models.CharField(max_length = 60)
id = models.IntegerField(primary_key = True)

class Meta:
managed = False
db_table ='bill'

将其保存到app' models.py 文件和运行迁移一切都很好。我可以读取DB,就像使用ORM创建的一样。但是,在Bill表中创建行有一个问题。



这是Bill模型的简单形式:

  class BillForm(ModelForm):

class Meta:
model = Bill
fields =('time','address')

问题是我无法检索使用DB序列生成的ID。将 id 字段添加到窗体将不起作用,因为我们必须使用代码生成它,然后作为参数传递。即使数据库将创建不同的ID,无法在没有原始查询的情况下检索。

解决方案

解决方案非常简单。我们必须从比尔模型中删除 id 字段:

  (models.Model):
time = models.DateField()
address = models.CharField(max_length = 60)
#删除'id'字段,以便ORM能正常工作。
#id = models.IntegerField(primary_key = True)

class Meta:
managed = False
db_table ='bill'

这样,Django ORM可以使所有的辛勤工作和正确使用旧数据库序列。现在可以使用BillForm创建Bill对象,它们将具有适当的ID。


Given a database table that was created using this SQL query:

CREATE TABLE Bill
  (
    Time DATE NOT NULL ,
    Address VARCHAR2 (60) NOT NULL ,
    ID           NUMBER NOT NULL
  ) ;

ALTER TABLE Bill ADD CONSTRAINT Bill_PK PRIMARY KEY ( ID ) ;

CREATE SEQUENCE Bill_ID_SEQ START WITH 1 NOCACHE ORDER ;
CREATE OR REPLACE TRIGGER Bill_ID_TRG BEFORE
  INSERT ON Paragony FOR EACH ROW BEGIN :NEW.ID := Bill_ID_SEQ.NEXTVAL;
END;

I have to use it with Django ORM so I have run inspectdb command. It is the autogenerated code:

class Bill(models.Model):
    time = models.DateField()
    address = models.CharField(max_length=60)
    id = models.IntegerField(primary_key=True)

    class Meta:
        managed = False
        db_table = 'bill'

After saving it to app' models.py file and running migrations everything was fine. I could read DB like it was created using ORM. However there was a problem with creating rows in Bill table.

It is simple form for Bill model:

class BillForm(ModelForm):

    class Meta:
        model = Bill
        fields = ('time', 'address')

The problem is that I can't retrieve the ID generated with the DB sequence. Adding id field to Form won't work because we have to generate it with code and then pass as a argument. Even than database will create different ID that will not be possible to retrieve without raw queries.

解决方案

The solution is really simple. We have to remove the id field from Bill model:

class Bill(models.Model):
    time = models.DateField()
    address = models.CharField(max_length=60)
    # Remove 'id' field so the ORM can work properly.
    # id = models.IntegerField(primary_key=True)

    class Meta:
        managed = False
        db_table = 'bill'

This way the Django ORM can make all the hard work and correctly use legacy database sequences. Bill objects now can be created using BillForm and they will have proper IDs.

这篇关于Django与遗留数据库 - 如何使用DB序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:37