Oh Great One
Oh Great One

Reputation: 384

Django Saving Form Instance to a Different Database

I am trying to save the instance of the form to a different database.

Usually when executing a save on an object instance you will use object.save(using='db_alias'), which I assumed would be for the form.save() function.

Currently I am using...

form.save(using='db_alias')

This throws an error claiming save() got an unexpected keyword argument 'using'.

Do I need to override the save() function within this particular form to handle a db_alias argument? I wasn't able to find anything regarding this error when searching, so I am asking here as last resort for the best foot forward. Thanks in advance.

Upvotes: 0

Views: 698

Answers (2)

Tyler
Tyler

Reputation: 1

Edit: You have to run makemigrations --database= to populate tables into the said db I order for instance = form.save(commit=False); instance.save(using=) to work.

Upvotes: 0

user8060120
user8060120

Reputation:

you are right , you can't save form such way, but you can use commit=False and then save the instance, for example:

instance = form.save(commit=False)
instance.save(using='db_alias')

Upvotes: 2

Related Questions