3

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

yann
  • 87
  • 2
  • 10

3 Answers3

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
  • 666
  • 3
  • 10
  • Hello ACGray thanks for your answer, but I have the same problem that with Daniel's one : I have an error like if I was not unsing "using='dataset' => (1146, "Table 'myproject.company_dataset' doesn't exist") – yann Mar 23 '14 at 21:24
  • Does the table exist in your 'dataset' database? `./manage.py syncdb` with no arguments only operates on the 'default' database. You can install all models into another database using `./manage.py syncdb --database=dataset`. If you want to constrain which models are available in which databases you need to write a database routing scheme. See https://docs.djangoproject.com/en/dev/topics/db/multi-db/#topics-db-multi-db-routing – ACGray Mar 24 '14 at 09:17
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')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hello Daniel, thanks for your answer I already tried that like you explain it here : http://stackoverflow.com/questions/6118884/django-model-form-save-multi-database-question but then I have an error like if I was not unsing "using='dataset' => (1146, "Table 'myproject.company_dataset' doesn't exist") – yann Mar 23 '14 at 20:51
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
  • 87
  • 2
  • 10