首页 > 解决方案 > AttributeError:“NoneType”对象没有属性“attname”

问题描述

我正在使用这个django_multitenant库来实现多租户。我尝试使用以下代码在 python manage.py shell 中创建一个对象

>>> from ReportingWebapp.models import *
>>> from django_multitenant.utils import *
>>> org = Organization.objects.first()
>>> set_current_tenant(org)
>>> get_current_tenant()
<Organization: Organization object (1)>
>>> a = ApplicationSetting(username="a",password="b",client_secret="c",client_id="d",tenant_id="e")

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django_multitenant\mixins.py", line 58, in __init__
    super(TenantModelMixin, self).__init__(*args, **kwargs)
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\base.py", line 416, in __init__
    self._state = ModelState()
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django_multitenant\mixins.py", line 62, in __setattr__
    if (attrname in (self.tenant_field, get_tenant_field(self).name)
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django_multitenant\mixins.py", line 115, in tenant_field
    return self.tenant_id
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\query_utils.py", line 149, in __get__
    instance.refresh_from_db(fields=[field_name])
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\base.py", line 623, in refresh_from_db
    db_instance_qs = self.__class__._base_manager.db_manager(using, hints=hints).filter(pk=self.pk)
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\base.py", line 573, in _get_pk_val
    return getattr(self, meta.pk.attname)
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\query_utils.py", line 147, in __get__
    val = self._check_parent_chain(instance)
  File "C:\Users\hp\Desktop\reporting_multitenant\env\lib\site-packages\django\db\models\query_utils.py", line 163, in _check_parent_chain
    return getattr(instance, link_field.attname)
AttributeError: 'NoneType' object has no attribute 'attname'

为什么我会收到此错误?租户已在线程本地设置。

模型.py

class Organization(TenantModel):
    tenant_id = 'id'
    org_name = models.CharField('Organization Name',max_length=200)
    num_agents = models.PositiveIntegerField(
        'Number of agents',
        help_text = 'Approximate number of agents who possess microsoft teams license'
        )
    admin_name = models.CharField(max_length=50)
    admin_email = models.EmailField()


class ApplicationSetting(TenantModel):
    org = models.ForeignKey(Organization,on_delete=models.CASCADE)
    tenant_id='organization_id'
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=100)
    client_id = models.CharField(max_length=255)
    client_secret = models.CharField(max_length=255)
    tenant_id = models.CharField(max_length=255, null=True)
    user_token = models.TextField(null=True)
    app_token = models.TextField(null=True)

标签: pythondjango

解决方案


我怀疑问题是由于您在模型中声明 的tenant_id 模型字段ApplicationSetting。在内部,您正在使用的包似乎tenant_id用作对租户相关外键字段名称的引用。

由于您声明tenant_id为可为空的字段,因此原始字段tenant_id='organization_id'不再存在。当你初始化你的ApplicationSettingTenantManager寻找一个名为的字段None并尝试找到它的相关名称(通过attname属性),因此错误。

要解决此问题,请删除该tenant_id字段。

之后,您可能会遇到不同的异常,因为您的ApplicationSetting模型不包含名为organization. 要解决此问题,您需要重命名org字段:

organization = models.ForeignKey(Organization,on_delete=models.CASCADE)'

或者,您可以tenant_id改为org_id改为。

免责声明:我对您使用的软件包一点也不熟悉。


推荐阅读