1👍
✅
You’ve made a fundamental error in Python, I believe, by trying to structure your class as you’ve seen in Django models.
This:
class FarmUserAuthentication(models.Model):
# ...
hash = bcrypt.hashpw(str(uuid.UUID4()), bcrypt.gensalt())
is a class variable, not an instance variable. Thus, the value is shared between all instances of the class.
If you want a unique value for each instance, the normal way to do this would be in the __init__
function. However, in Django you should not override __init__
, so you should simply add a function to your model that creates your hash. Perhaps something like this:
class FarmUserAuthentication(models.Model):
def get_hash():
return bcrypt.hashpw(str(uuid.UUID4()), bcrypt.gensalt())
Or because you can’t call things easily in Django templates, perhaps a property:
class FarmUserAuthentication(models.Model):
@property
def hash():
return bcrypt.hashpw(str(uuid.UUID4()), bcrypt.gensalt())
Source:stackexchange.com