ValueError:无法分配必须是实例

时间:2017-12-20 20:10:56

标签: django

我有以下预先存在的数据库表,其中包含id和id以及描述。我必须在我的User表之前加载它才能正确关联ForeignKey。

class QVDSSSecurityDimension(models.Model):

    coid = models.CharField(db_column='CM_Data_Restriction', serialize=False, max_length=10, primary_key = True)  # Field name made lowercase.
    coid_name = models.CharField(db_column='CM_Company_Name', max_length=50, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'QV_DSS_Security_Dimension'

我的自定义用户模型基于以下内容构建:

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True)
    username = models.CharField(max_length=7, unique=True)
    formattedusername = models.CharField(max_length=11, unique=True, primary_key = True)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=140)
    date_joined = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_cfo = models.BooleanField(default=False)
    facility = models.CharField(max_length=140)
    officename = models.CharField(max_length=100)
    jobdescription = models.CharField(max_length=140)
    positioncode = models.CharField(max_length = 100)
    positiondescription = models.CharField(max_length=140)
    coid = models.ForeignKey(QVDSSSecurityDimension, null=True, blank = True, db_constraint=False)
    streetaddress = models.CharField(max_length=140)
    title = models.CharField(max_length=100)

    USERNAME_FIELD = 'username'

    class Meta:
        app_label = 'accounts'
        db_table = "user"

    def save(self, *args, **kwargs):
        self.formattedusername = '{domain}\{username}'.format(
            domain='HCA', username=self.username)
        super(User, self).save(*args, **kwargs);

    def get_short_name(self):
        return self.username

#    REQUIRED_FIELDS = "username"

    def __str__(self):
        return '%s - %s %s' % (self.username, self.first_name, self.last_name)

所有内容都适用于makemigrations并进行迁移,但如果数据库中不存在User.coid,则在尝试登录时会出现以下错误:

ValueError: Cannot assign "'08732'": "User.coid" must be a "QVDSSSecurityDimension" instance.

我确认QID确实存在于QVDSSSecurityDimension中,但由于表中没有带有coid的用户,因此它会抛出该错误。如果用户使用AD验证并且自定义用户表中不存在其coid,如何使登录仍能正常运行?

我尝试使用db_constraint = True时Null = True和Blank = True,似乎没有任何效果。用户存储到数据库后,coid将存在,但此错误发生在此之前。

这是我的个人资料视图

def profile(request):
    owner = User.objects.get (formattedusername=request.user.formattedusername)
    reportdetail = QVReportAccess.objects.filter(ntname = owner.formattedusername, active = 1).values('report_name_sc')

    reportIds = QVReportAccess.objects.filter(ntname = owner.formattedusername).values_list('report_id', flat=True)
    reportaccess = QvReportList.objects.filter(report_id__in= reportIds).values_list('report_name_sc', flat = True).distinct()
    reportGroups = QVReportAccess.objects.filter(ntname = owner.formattedusername).values_list('report_group_id', flat=True)
    reportlist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).exclude(active=0)

    allreports = 'All Reports'

    if allreports in reportaccess:
        bhreportgrouplist = None
        cereportgrouplist = None
        finreportgrouplist = None
        careportgrouplist = None
        pireportgrouplist = None
        screportgrouplist = None
        dssreportgrouplist = None
        psgreportgrouplist = None
        othreportgrouplist = None
        showbutton = None
    else:
        bhreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 200)
        cereportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 500)
        finreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 600)
        careportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 800)
        pireportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1100)
        screportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1200)
        dssreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1300)
        psgreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1400)
        othreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 99999)
        showbutton = ""



    args = {'user':owner, 'applicationaccess':reportaccess, 'applicationlist':reportlist, 'bhgrouplist':bhreportgrouplist, 'cegrouplist':cereportgrouplist, 'fingrouplist':finreportgrouplist
          , 'cagrouplist':careportgrouplist, 'pigrouplist':pireportgrouplist, 'scgrouplist':screportgrouplist, 'dssgrouplist':dssreportgrouplist, 'psggrouplist':psgreportgrouplist
          , 'othgrouplist':othreportgrouplist, 'showbutton':showbutton}

    return render(request, 'accounts/profile.html', args)

我可以从没有DSSSecurityDimension的我的formattedusername获取coid,因为它从AD中提取它,但是我需要来自DSSSecurityDimension的描述,这就是我在配置文件模板上引入coid的原因,但是来自User.coid不是DSSSecurityDimension.coid。

我的模板中的Coid使用以下li

进行渲染
<li>Coid: {{ user.coid }}</li>

代码行在python库中,而不是我的视图,这告诉我它的模型存在问题:

Traceback (most recent call last):
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 54, in inner
    return func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 150, in login
    )(request)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_w
rapper
    return view(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\views.py", line 90, in dispatch
    return super(LoginView, self).dispatch(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\edit.py", line 182, in post
    if form.is_valid():
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 175, in errors
    self.full_clean()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 385, in full_clean
    self._clean_form()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\forms.py", line 412, in _clean_form
    cleaned_data = self.clean()
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\forms.py", line 187, in clean
    self.user_cache = authenticate(self.request, username=username, password=password)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\__init__.py", line 70, in authenticate
    user = _authenticate_with_backend(backend, backend_path, request, credentials)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\__init__.py", line 115, in _authenticate_with_backend
    return backend.authenticate(*args, **credentials)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\auth.py", line 23, in authenticate
    return ldap.authenticate(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 205, in authenticate
    return c.get_user(**kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 115, in get_user
    return self._get_or_create_user(self._connection.response[0])
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django_python3_ldap\ldap.py", line 67, in _get_or_create_user
    **user_lookup
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 482, in update_or_create
    obj, created = self._create_object_from_params(lookup, params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 498, in _create_object_from_params
    obj = self.create(**params)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 392, in create
    obj = self.model(**kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\base_user.py", line 68, in __init__
    super(AbstractBaseUser, self).__init__(*args, **kwargs)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 554, in __init__
    _setattr(self, field.name, rel_obj)
  File "C:\Users\HFA9592\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 216, in __set__
    self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'08732'": "User.coid" must be a "QVDSSSecurityDimension" instance.

登录在我的settings.py

中处理

以下内容:

AUTHENTICATION_BACKENDS = (
    'django_python3_ldap.auth.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

AUTH_USER_MODEL = "accounts.User"

# The URL of the LDAP server.
LDAP_AUTH_URL = "ldap://ip of server"


# Initiate TLS on connection.
LDAP_AUTH_USE_TLS = False

# The LDAP search base for looking up users.
LDAP_AUTH_SEARCH_BASE = "DC=domainname,DC=corpad,DC=net"

LDAP_AUTH_OBJECT_CLASS = "user"

# User model fields mapped to the LDAP
# attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
    "username": "sAMAccountName",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
    "coid": "extensionAttribute10",
    "facility": "company",
    "officename":"physicalDeliveryOfficeName",
   "streetaddress": "streetAddress",
    "jobdescription":"corpadNet2001-CORPds-JobCodeDescription",
    "positioncode":"corpadNet2001-CORPds-PositionCode",
    "positiondescription":"corpadNet2001-CORPds-PositionCodeDescription",
    "title":"title",
}

# A tuple of django model fields used to uniquely identify a user.
LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

# Path to a callable that takes a dict of {model_field_name: value},
# returning a dict of clean model data.
# Use this to customize how data loaded from LDAP is saved to the User model.
LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"

# Path to a callable that takes a user model and a dict of {ldap_field_name: [value]},
# and saves any additional user relationships based on the LDAP data.
# Use this to customize how data loaded from LDAP is saved to User model relations.
# For customizing non-related User model fields, use LDAP_AUTH_CLEAN_USER_DATA.
LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

# Path to a callable that takes a dict of {ldap_field_name: value},
# returning a list of [ldap_search_filter]. The search filters will then be AND'd
# together when creating the final search filter.
LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters"

# Path to a callable that takes a dict of {model_field_name: value}, and returns
# a string of the username to bind to the LDAP server.
# Use this to support different types of LDAP server.
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory"

1 个答案:

答案 0 :(得分:1)

我想我看到了问题。您希望User的{​​{1}}字段包含相关coid对象的实际ID,但这不是QVDSSSecurityDimension默认情况下的工作方式。

ForeignKey的{​​{1}}字段实际上是仅存在于Python逻辑中的属性。在数据库级别,User表中没有coid列。这是因为Django创建了另一个未在模型中显式声明的字段,名为coid,用于保存相关对象的ID。 是数据库中实际存在的列和值。

换句话说,如果列出User对象的属性,您会看到它有两个字段:coid_idUsercoid将检索coid_id的实例,而coid将保留您尝试设置的实际值。

检查Django's documentationThis post还可以帮助您了解QVDSSSecurityDimensioncoid_id的工作方式。

这是我认为您正在寻找的:

coid

我将coid_id重命名为class User(AbstractBaseUser, PermissionsMixin): ... co = models.ForeignKey(QVDSSSecurityDimension, null=True, blank=True) ... 以使其更直观,因为它不会保留实际ID。我还删除了coid,因为默认co应该可以正常工作。

当您尝试将db_constraint=False设置为您的用户时,您可以执行以下操作:

db_constraint=True

或:

QVDSSSecurityDimension

或:

some_user = User.objects.first()
some_dimension = QVDSSSecurityDimension.objects.get(coid='08732')

some_user.co = some_dimension
some_user.save()

看到区别?我希望这有助于澄清一些事情。 :)

修改

具体来说,问题出在设置变量some_user = User.objects.first() some_dimension = QVDSSSecurityDimension.objects.get(coid='08732') some_user.co_id = some_dimension.coid some_user.save() 中,它试图将some_user = User.objects.first() some_dimension_id = '08732' some_user.co_id = some_dimension_id some_user.save() 内的ID(从LDAP_AUTH_USER_FIELDS收到的数据)映射到extensionAttribute10&# 39; s LDAP字段,它预期User个实例。解决方案是将coid中的密钥从QVDSSSecurityDimension更改为LDAP_AUTH_USER_FIELDS