Search code examples
pythondjangomulti-database

save() got an unexpected keyword argument 'using'


I'm using a multi database system for my django project.

But when I'm trying to save my form, I get this error : save() got an unexpected keyword argument 'using'

Here is my simple code :

My View :

def addCompany2(request):
"""Add a company"""

    selectedObject = CompanyDataset()

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

return render_to_response('company/addCompany2.html', {'referer': referer, 'formCompany2': formCompany2}, context_instance=RequestContext(request))

my model

class CompanyDataset(models.Model):
    name = models.CharField(max_length=255, blank=True)
    ....
    ...
    ...

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'company_dataset'
        managed = True

my form :

class CompanyForm2(ModelForm):

    class Meta:
        model = CompanyDataset
        #exclude = ('website')

    def __init__(self, *args, **kwargs):
        super(CompanyForm2, self).__init__(*args, **kwargs)

        self.fields.keyOrder = [
            'nom',
            'country'
            ]

settingsLocal

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'XXXXX',
        'USER': 'XXXXX',
        'PASSWORD': 'XXXXX',
        'HOST': '127.0.0.1',
        'PORT': '',
    },
    'dataset': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'XXXXX',
        'USER': 'XXXXX',
        'PASSWORD': 'XXXXX',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Refering to the documentation : https://docs.djangoproject.com/en/1.6/topics/db/multi-db/ I dont understand why it's not working? I'probably did something wrong but I can't find what.

My django version is : 1.6.2

Thanks in advance for your help. :)


Solution

  • 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 :)