title: Django, how do you set the value of a field in a model to be a hash of one of the fields?
body: <p>This is what my model looks like:</p>
  
  <pre><code>class Alert(models.Model):
      hash = models.CharField(max_length=64, unique=True)
      raw_line = models.TextField()
      alert_datetime = models.DateTimeField()
      datetime_dismissed = models.DateTimeField(null=True)
  </code></pre>
  
  <p>and when I create an Alert, I set the hash value as something. But, I read that I can effectively do this:</p>
  
  <pre><code>hash = models.CharField(max_length=64, unique=True, default=some_hash_function)
  
  def some_hash_function():
      ...
      return some_hash
  </code></pre>
  
  <p>But the hash I want relies on raw_line, is it possible to have the function hash the raw_line and set it by default?</p>
  
  <p>Some additional context:</p>
  
  <p>I've tried doing the following:</p>
  
  <pre><code>class Alert(models.Model):
      hash = models.CharField(max_length=64, unique=True, default=hash_raw_line)
      raw_line = models.TextField()
      alert_datetime = models.DateTimeField()
      datetime_dismissed = models.DateTimeField(null=True)
  
      def hash_raw_line(self):
          return hashlib.sha256(self.raw_line).hexdigest()
  </code></pre>
  
  <p>but it seems like the it can't find the reference for hash_raw_line </p>