Coding_line
Coding_line

Reputation: 127

Syncing db with existing tables through django for an existing schema table and also updating few columns for the tables and the rest automatically

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.

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

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

Steps for 1

What i have done as of now is created a class in model.py with attributes as 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.

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.

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

Steps for 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

Upvotes: 1

Views: 1293

Answers (1)

Charlie
Charlie

Reputation: 2231

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.

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.

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

Upvotes: 1

Related Questions