0

i need to update the columnvalue of a particular field everytime i create a new entry to the table with some hash value. i created the model as follows:

class ICOExchange(models.Model):

    id = models.CharField(default=create_UUID('user '),
                          max_length=100, primary_key=True)


    def set_id(self):
        self.id = create_UUID('user ')

where create_UUID(arg) is a function that generates a hash value according to some inputs and timestamp. But while adding datasets through the admin panel it doesnt refresh the id and id remains the same.

Ayush
  • 57
  • 2
  • 11
  • Chaging the `id` is a very strange use case, typically it is better to let the `id` remain *fixed*, at all times. Perhaps you can add an extra column. – Willem Van Onsem Sep 25 '18 at 12:45
  • The fact that the value remains fixed, is the expected behavior: since (a) `default` is mean as a way to specify an inital value, and (b) here you set as default the *value* that comes out of the `create_UUID` call, you do *not* call the value very time. – Willem Van Onsem Sep 25 '18 at 12:46
  • Shouldnt the id change for each new data entry as it is recalculated again and again? – Ayush Sep 25 '18 at 12:58
  • 1
    well Django already automatically create's an `id` here. If you want to perform hashing of data, you better construct an *extra* column, and furthermore override the `save(..)` method. But even then, you can circumvent updating with for example `Model.objects.filter(foo=bar).update(qux=14)`. – Willem Van Onsem Sep 25 '18 at 12:59
  • don't call the method try this `id = models.CharField(default=create_UUID, max_length=100, primary_key=True)` – Vaibhav Vishal Sep 25 '18 at 13:06

1 Answers1

0

create_UUID('user ') is called when the python file is loaded (when the server start), then default keep the same value.

A solution is :

default=create_UUID

but you have no parameter. Maybe with a lambda you can set a parameter ?

Look this question, this is the same but with datetime : Django datetime issues (default=datetime.now())