如何将用户模型传递到表单字段(django)?

如何将用户模型传递到表单字段(django)?,django,encryption,field,formfield,Django,Encryption,Field,Formfield,基本上,我需要使用用户的密码哈希来通过自定义模型字段加密一些数据。请查看我在此处使用的代码片段: 我试过这个: class MyClass(models.Model): owner = models.ForeignKey(User) product_id = EncryptedCharField(max_length=255, user_field=owner) ..........................................................

基本上,我需要使用用户的密码哈希来通过自定义模型字段加密一些数据。请查看我在此处使用的代码片段:

我试过这个:

class MyClass(models.Model): owner = models.ForeignKey(User) product_id = EncryptedCharField(max_length=255, user_field=owner) ................................................................................. def formfield(self, **kwargs): defaults = {'max_length': self.max_length, 'user_field': self.user_field} defaults.update(kwargs) return super(EncryptedCharField, self).formfield(**defaults)) 类MyClass(models.Model): 所有者=型号。外键(用户) product\u id=EncryptedCharField(最大长度=255,用户\u字段=owner) ................................................................................. def表单字段(自身,**kwargs): 默认值={'max\u length':self.max\u length,'user\u field':self.user\u field} 默认值。更新(kwargs) 返回super(EncryptedCharField,self).formfield(**默认值)) 但当我尝试使用user_字段时,我得到了一个ForeignKey实例(当然!):

user\u field=kwargs.get('user\u field') 密码=用户\字段。密码[:32]
感谢您的帮助

可能是这样的-重写save()方法,在这里可以调用encrypt方法

对于decrypt,您可以使用,因此每次从数据库实例化模型时,product_id字段都会自动解密

class MyClass(models.Model):
    user_field = models.ForeignKey(User)
    product_id = EncryptedCharField()
    ...other fields...

    def save(self):
        self.product_id._encrypt(product_id, self.user_field)
        super(MyClass,self).save()

    def decrypt(self):
        if self.product_id != None:
            user = self.user_field
            self.product_id._decrypt(user=user)

def post_init_handler(sender_class, model_instance):
    if isinstance(model_instance, MyClass):
        model_instance.decrypt()

from django.core.signals import post_init
post_init_connect.connect(post_init_handler)


obj = MyClass(user_field=request.user) 
#post_init will be fired but your decrypt method will have
#nothing to decrypt, so it won't garble your input
#you'll either have to remember not to pass value of crypted fields 
#with the constructor, or enforce it with either pre_init method 
#or carefully overriding __init__() method - 
#which is not recommended officially

#decrypt will do real decryption work when you load object form the database

obj.product_id = 'blah'
obj.save() #field will be encrypted

也许有一种更优雅的“pythonic”方法可以做到这一点

也许类似于这样——重写save()方法,在这里可以调用encrypt方法

对于decrypt,您可以使用,因此每次从数据库实例化模型时,product_id字段都会自动解密

class MyClass(models.Model):
    user_field = models.ForeignKey(User)
    product_id = EncryptedCharField()
    ...other fields...

    def save(self):
        self.product_id._encrypt(product_id, self.user_field)
        super(MyClass,self).save()

    def decrypt(self):
        if self.product_id != None:
            user = self.user_field
            self.product_id._decrypt(user=user)

def post_init_handler(sender_class, model_instance):
    if isinstance(model_instance, MyClass):
        model_instance.decrypt()

from django.core.signals import post_init
post_init_connect.connect(post_init_handler)


obj = MyClass(user_field=request.user) 
#post_init will be fired but your decrypt method will have
#nothing to decrypt, so it won't garble your input
#you'll either have to remember not to pass value of crypted fields 
#with the constructor, or enforce it with either pre_init method 
#or carefully overriding __init__() method - 
#which is not recommended officially

#decrypt will do real decryption work when you load object form the database

obj.product_id = 'blah'
obj.save() #field will be encrypted

也许有一种更优雅的“蟒蛇式”方法可以做到这一点

首先,感谢您的回复!希望我能想出类似的东西。但是,你有没有关于如何通过信号来实现这一点的基本例子?我对整个信号的了解非常有限…谢谢!哇,我真的很感动。我以后一定会把它转一转。再次感谢!首先,感谢您的回复!希望我能想出类似的东西。但是,你有没有关于如何通过信号来实现这一点的基本例子?我对整个信号的了解非常有限…谢谢!哇,我真的很感动。我以后一定会把它转一转。再次感谢!