本文介绍了ValueError:无法分配必须是实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下预先存在的数据库表,其中包含和id以及说明.我必须在User表之前加载此文件,以便正确关联ForeignKey.的方式代码>默认情况下有效.

I think I see the problem. You expect User's coid field to contain the actual ID of the related QVDSSSecurityDimension object, but this is not how ForeignKey works by default.

User coid 字段实际上是仅在您的Python逻辑内部存在的属性.在数据库级别, User 表中没有 coid 列.这是因为Django创建了另一个未在模型中明确声明的字段,称为 coid_id 来保存相关对象的ID.那是数据库中实际存在的列和值.

The User's coid field is actually an attribute that exists only inside your Python logic. At database level, there's no coid column in the User table. This is because Django creates another field that is not explicitly declared in your model, called coid_id, to hold the related object's ID. That is the column and value that actually exists in the database.

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

In other words, if you list the attributes of a User object, you'll see it has two fields: coid and coid_id. coid will retrieve an instance of QVDSSSecurityDimension, while coid_id will hold the actual value you're trying to set.

为此检查 Django的文档.这篇文章还可以帮助您了解 coid coid_id 的工作方式.

Check Django's documentation regarding this. This post can also help you understand how coid and coid_id work.

这是我认为您要寻找的:

This is what I think you're looking for:

class User(AbstractBaseUser, PermissionsMixin):
    ...
    co = models.ForeignKey(QVDSSSecurityDimension, null=True, blank=True)
    ...

我将 coid 重命名为 co ,以使其更加直观,因为它没有实际的ID.我还删除了 db_constraint = False ,因为默认的 db_constraint = True 应该可以正常工作.

I renamed coid to co to make it more intuitive, since it doesn't hold the actual ID. I also removed db_constraint=False, since the default db_constraint=True should work just fine.

当您尝试为用户设置 QVDSSSecurityDimension 时,您可以执行以下操作:

And when you try to set the QVDSSSecurityDimension to your user, you can either do:

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

看到区别了吗?我希望这可以使事情有所清除.:)

See the difference? I hope this helps clear things up a bit. :)

编辑

具体来说,问题出在设置变量 LDAP_AUTH_USER_FIELDS 中,该变量试图将ID映射到 extensionAttribute10 内部(从 LDAP 接收到的数据)到 User coid 字段,该字段需要一个 QVDSSSecurityDimension 实例.解决方案是将 LDAP_AUTH_USER_FIELDS 中的密钥从 coid 更改为 coid_id .

Specifically, the problem was in the settings variable LDAP_AUTH_USER_FIELDS, that was trying to map the ID inside extensionAttribute10 (data received from LDAP) to the User's coid field, which expected a QVDSSSecurityDimension instance. The solution was to change that key in the LDAP_AUTH_USER_FIELDS from coid to coid_id.

这篇关于ValueError:无法分配必须是实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:16
查看更多