How to generate HASH for Django model

后端 未结 2 1957
萌比男神i
萌比男神i 2021-01-04 20:59

I am trying to generate unique HASH values for my Django models of 10 digit i have tried these methods but i am getting this error

return Database.Cursor.ex         


        
相关标签:
2条回答
  • 2021-01-04 21:26

    _createHash() is called when you define the model, so you have the same default every time you create a new object.

    You can look at creating the hash in the save() method of the model, that's probably the easiest.

    0 讨论(0)
  • 2021-01-04 21:28

    Don't call the _createHash() function in your field, but just pass the reference to the callable in your model, e.g.

    hash_1 = models.CharField(max_length=10,default=_createHash,unique=True)
    

    As Lennart Regebro mentioned in his answer, you'll get the same value for each time you start the server in your attempt.

    The Django docs say this about it:

    Field.default

    The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

    0 讨论(0)
提交回复
热议问题