[Django]-Save() got an unexpected keyword argument 'using'

1👍

The solution was to create a database user : https://docs.djangoproject.com/en/dev/topics/db/multi-db/#database-routers

Here is the working exemple liked to the exemple I gave in my initial question :

dbRouter.py

import django
from company.models import CompanyDataset


class CompanyDatasetRouter(object):

    def db_for_read(self, model, **hints):
        #if isinstance(model, CompanyDataset):
        if model == CompanyDataset:
            return 'dataset'
        else:
            return 'default'

    def db_for_write(self, model, **hints):
        if model == CompanyDataset:
            return 'dataset'
        else:
            return 'default'

Thanks to all for your help 🙂

👤yann

3👍

The using= keyword argument is on the model save() method, not the ModelForm save method. You should do this instead:

...

if request.method == 'POST':
    formCompany2 = CompanyForm2(request.POST, instance=selectedObject)
    selectedObject = formCompany2.save(commit=False)
    selectedObject.save(using='dataset')

...
👤ACGray

2👍

The documentation doesn’t say anything about a using argument for a form’s save method. There’s one for a model save, though. So you can get the model object by saving with commit=False, then save it with using:

selectedObject = formCompany2.save(commit=False)
selectedObject.save(using='dataset')

Leave a comment