在Django中保存动态模型对象的问题(带有django-tenant-schema)

问题描述 投票:1回答:1

我在为Django应用动态生成用户时遇到问题,我在其中使用django-tenant-schemas进行多租户。

以下是我的观点摘要:

def RegisterView(request):
if request.method == 'POST':
    response_data = {}
    domain = request.POST.get('domain')
    schema = request.POST.get('schema')
    name = request.POST.get('name')
    description = request.POST.get('description')

    client=Client()
    Client.domain_url=domain
    Client.schema_name=schema
    Client.name=name
    Client.description=description
    Client.save()

现在,我使用在单独的应用程序客户下导入模型Client

from customers.models import Client

对于此多租户程序包,所有模型都继承基本的TenantMixin。

以下是我的模特:

class Client(TenantMixin):
name = models.CharField(max_length=100)
description = models.TextField(max_length=200)
created_on = models.DateField(auto_now_add=True)

这是TenanMixin代码段,其中包含您可以使用的帮助命令:

class TenantMixin(models.Model):
"""
All tenant models must inherit this class.
"""

auto_drop_schema = False
"""
USE THIS WITH CAUTION!
Set this flag to true on a parent class if you want the schema to be
automatically deleted if the tenant row gets deleted.
"""

auto_create_schema = True
"""
Set this flag to false on a parent class if you don't want the schema
to be automatically created upon save.
"""

domain_url = models.CharField(max_length=128, unique=True)
schema_name = models.CharField(max_length=63, unique=True,
                               validators=[_check_schema_name])

class Meta:
    abstract = True

def save(self, verbosity=1, *args, **kwargs):
    is_new = self.pk is None

    if is_new and connection.schema_name != get_public_schema_name():
        raise Exception("Can't create tenant outside the public schema. "
                        "Current schema is %s." % connection.schema_name)
    elif not is_new and connection.schema_name not in (self.schema_name, get_public_schema_name()):
        raise Exception("Can't update tenant outside it's own schema or "
                        "the public schema. Current schema is %s."
                        % connection.schema_name)

    super(TenantMixin, self).save(*args, **kwargs)

    if is_new and self.auto_create_schema:
        try:
            self.create_schema(check_if_exists=True, verbosity=verbosity)
        except:
            # We failed creating the tenant, delete what we created and
            # re-raise the exception
            self.delete(force_drop=True)
            raise
        else:
            post_schema_sync.send(sender=TenantMixin, tenant=self)

现在,我遇到的错误是:

save() missing 1 required positional argument: 'self'

回溯是:

Traceback:  

File "/home/sayantan/Desktop/djangowork/my_env/lib/python3.4/site- packages/django/core/handlers/base.py" in get_response
  149.                     response =     self.process_exception_by_middleware(e, request)

File "/home/sayantan/Desktop/djangowork/my_env/lib/python3.4/site-    packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request,  *callback_args, **callback_kwargs)

File "/home/sayantan/Desktop/djangowork/invoice/invoice/views.py" in RegisterView
  55.         Client.save()

Exception Type: TypeError at /register/
Exception Value: save() missing 1 required positional argument: 'self'

如果可能,请您帮助我。

python django dynamic model multi-tenant
1个回答
0
投票

这可能是因为您要在client变量上实例化Client模型,而不是使用类的实例,而是将数据直接分配给类。您应该保存()您的对象,而不是类本身(如herehere

def RegisterView(request):
    if request.method == 'POST':
    response_data = {}
    domain = request.POST.get('domain')
    schema = request.POST.get('schema')
    name = request.POST.get('name')
    description = request.POST.get('description')

    client=Client()
    client.domain_url=domain
    client.schema_name=schema
    client.name=name
    client.description=description
    client.save()
© www.soinside.com 2019 - 2024. All rights reserved.