Reputation: 2926
I have the following Django model
class Labels(models.Model):
user = models.CharField(max_length=200)
label = models.CharField(max_length=200)
live = models.CharField(max_length=1)
unique_key = models.CharField(max_length=200)
def __str__(self):
return '%s / %s' % (self.user, self.label)
I would like unique_key
to be automatically populated with a concatenation of md5(user + label)
e.g.
user
= 'James'
label
= 'KDJ'
concat = user + label
unique_key = print(hashlib.md5(concat.encode()).hexdigest())
Output
1935636b374a17f87636460e4307f736
Upvotes: 0
Views: 115
Reputation: 47364
You can override save method for this:
class Labels(models.Model):
def save(self, *args, **kwargs):
concat = self.user + self.label
self.unique_key = hashlib.md5(concat.encode()).hexdigest()
super().save(*args, **kwargs)
Upvotes: 2