2016-05-31 39 views
1

我在動態生成我的Django應用程序的用戶時遇到了問題,我在這裏爲多租戶使用了django-tenant-schemas。在Django中保存動態模型對象的問題(使用djano-tenant-schema)

以下是我的看法片段:

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() 

現在,我使用

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' 

如果可能,請求您幫助我。

+0

我能取悅請求的答覆,或許我們可以討論和解決,bcoz其kindaa急!!! – Sayantan

回答

0

這可能發生,因爲你是你的客戶端上實例您客戶模式變量,但使用的類的實例,你直接到指定類的數據。你應該保存()你的對象,而不是類本身(因爲你可以看到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() 
+0

ohhh yesss !!!!!這樣一個愚蠢的錯誤! yeaa ....必須是! Thanxx Filipe – Sayantan

+0

@Sangs很高興幫助請接受答案,如果幫助:) –

+0

嘿Fil!我做的!!!真的很抱歉,我正在旅行,網絡連接不好!但thnxxx再次男人 – Sayantan