通过django将db与现有表同步到现有模式表,并自动更新表和其余列的几列

时间:2023-01-03 02:57:59

I am doing a poc in Django and i was trying to create the admin console module for inserting,updating and deleting records through django admin console through models and it was doing fine I have 2 questions.

我正在Django做一个poc,我试图创建管理控制台模块,通过django管理控制台通过模型插入,更新和删除记录,它做得很好我有2个问题。

1.I need to have model objects for existing tables which needs to be present in a particular schema.say schema1.table1 Here as of now i was doing poc for public schema. So can it be done in a fixed defined schema and if yes how.Any reference would be very helpful

1.我需要为现有表格提供模型对象,这些表格需要存在于特定的schema.say schema1.table1这里截至目前我正在为公共模式做poc。所以它可以在一个固定的定义模式中完成,如果是的话。任何引用都会非常有用

2.Also i wanted to update few columns in the table through console and the rest of the columns will be done automatically like currentimestamp and created date etc.Is it possible through default django console and if yes kindly share any reference

2.此外,我想通过控制台更新表中的几列,其余列将自动完成,如currentimestamp和创建日期等。这是可能通过默认的django控制台,如果是,请分享任何参考

Steps for 1

步骤1

What i have done as of now is created a class in model.py with attributes as author,title,body,timeofpost

我现在所做的是在model.py中创建了一个类,其属性为author,title,body,timeofpost

Then i used sqlmigrate after makemigrations app to create the table and after migrating have been using the admin console for django to insert and update the records for the table created.But this is for POC only.

然后我在makemigrations app之后使用sqlmigrate来创建表,并且在迁移之后一直使用django的管理控制台来插入和更新创建的表的记录。但这仅适用于POC。

Now i need to do the same but for existing tables with whom i can interact and insert or update record for those existing tables through admin console.

现在我需要做同样的事情但是对于我可以与之交互的现有表,并通过管理控制台插入或更新那些现有表的记录。

Also the tables are getting created in public schema by default.But i am using postgres and the existing tables are present in different schemas and i wanted to insert,update and delete for this existing tables.

此外,默认情况下,这些表是在公共模式中创建的。但我使用的是postgres,现有的表存在于不同的模式中,我想为这些现有表插入,更新和删除。

I am stuck up here as i dont know how to configure model with existing database schema tables through which we can interact through django console and also for different schemas and not in public schema

我被困在这里因为我不知道如何使用现有的数据库模式表配置模型,通过它我们可以通过django控制台和不同的模式进行交互,而不是在公共模式中

Steps for 2:

步骤2:

Also i wanted the user to give input for few columns like suppose in this case time of creation is not required to be given as input by user .Rather it should be taken care when the database is updating or creating

此外,我希望用户为少数列提供输入,例如假设在这种情况下创建时间不需要由用户输入。在数据库更新或创建时应该小心。

Thanks

谢谢

1 个解决方案

#1


1  

In order for Django to "interact" with an existing database you need to create a model for it which can be done automatically as shown here. This assumes that your "external" database isn't going to be changed often because you'll have to keep your models in sync which is tricky - there are other approaches if you need that.

为了让Django与现有数据库“交互”,您需要为它创建一个模型,可以自动完成,如下所示。这假设您的“外部”数据库不会经常更改,因为您必须保持模型同步这很棘手 - 如果您需要,还有其他方法。

As for working with multiple database schemas - is there a reason you can't put your POC table in the same database as the others? Django supports multiple databases, but it will be harder to setup. See here.

至于使用多个数据库模式 - 有没有理由不能将POC表放在与其他数据库相同的数据库中? Django支持多个数据库,但设置起来会更困难。看这里。

Finally, it sounds like you are interested in setting the Django default field attribute. For an example of current time see here.

最后,听起来您有兴趣设置Django默认字段属性。有关当前时间的示例,请参见此处。

#1


1  

In order for Django to "interact" with an existing database you need to create a model for it which can be done automatically as shown here. This assumes that your "external" database isn't going to be changed often because you'll have to keep your models in sync which is tricky - there are other approaches if you need that.

为了让Django与现有数据库“交互”,您需要为它创建一个模型,可以自动完成,如下所示。这假设您的“外部”数据库不会经常更改,因为您必须保持模型同步这很棘手 - 如果您需要,还有其他方法。

As for working with multiple database schemas - is there a reason you can't put your POC table in the same database as the others? Django supports multiple databases, but it will be harder to setup. See here.

至于使用多个数据库模式 - 有没有理由不能将POC表放在与其他数据库相同的数据库中? Django支持多个数据库,但设置起来会更困难。看这里。

Finally, it sounds like you are interested in setting the Django default field attribute. For an example of current time see here.

最后,听起来您有兴趣设置Django默认字段属性。有关当前时间的示例,请参见此处。