【问题标题】:Save using multiple databases使用多个数据库保存
【发布时间】:2020-09-03 02:41:29
【问题描述】:

我正在使用多个数据库

DATABASES = {
    'default': {
        # for local postgre databases
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '2003',
        'USER' : 'postgres',
        'PASSWORD' : '3211',
        'HOST' : 'localhost',
    },
    'rana@rana.com': {
        # for local postgre databases
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'rana@rana.com',
        'USER' : 'postgres',
        'PASSWORD' : '3211',
        'HOST' : 'localhost',
    }
}

现在在使用 inlineformset_factory 时,我正在尝试保存(使用=rana@rana.com) 但出现错误 “save() 有一个意外的关键字参数‘使用’” 我在views.py中使用的地方

def panelList (request, id=None):
    detail      = Panel.objects.using(username).filter(user=request.user).order_by('detail')
    buttonlable =   "Panel List"
    heading     =   "Panel List"
    formset     =   modelformset_factory(Panel, extra=1, max_num=1, can_delete=False,
                fields=('user','detail','panel_type','billing_address','contact','contact_person','status','email',),
                labels={'user':'','detail':'','panel_type':'','billing_address':'','contact':'','contact_person':'','status':'','email':'',}, 
                widgets={
                'user'    :   forms.HiddenInput(attrs={'placeholder':'user','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true', 'value':user,}), 
                'detail'    :   forms.TextInput(attrs={'placeholder':'Name','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'panel_type'    :   forms.TextInput(attrs={'placeholder':'Type','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'billing_address'    :   forms.TextInput(attrs={'placeholder':'Address','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'contact'    :   forms.TextInput(attrs={'placeholder':'Contact','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'contact_person'    :   forms.TextInput(attrs={'placeholder':'Contact Person','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'email'    :   forms.TextInput(attrs={'placeholder':'email','class':'form-control col-sm mr-1 mt-1 border-muted bg-light border border-info','required':'true'}), 
                'status':   forms.HiddenInput(attrs={'placeholder':'status', 'class':'form-control col-sm mr-1 mt-1 bg-light border-muted'})})
    if request.method == "POST":
        formset = formset(request.POST)
        for form in formset:
            if form.is_valid:
                form.save(commit=False)
                form.user = request.user
                form.save(using=rana@rana.com)
        return redirect("panelList")

【问题讨论】:

    标签: save-as multi-database multi-database-connections


    【解决方案1】:

    您需要引用数据库名称,例如。 form.save(使用='rana@rana.com')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2020-05-11
      • 1970-01-01
      • 2019-06-05
      • 2021-06-09
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多