title: Django - Function inside a model. How to call it from a view?
body: <p>I'm designing a model in Django but I don't know if this is the best way. I have a model called "History" and inside this model I've a specialized function that will handle the inserts to this model.</p>
  
  <p><strong>Alternative 1</strong></p>
  
  <pre><code>class History(models.Model):
      field1 = models.ForeignKey(Request)
      field2 = models.BooleanField()
      field3 = models.DateTimeField()
  
      def __unicode__(self):
          return str(self.field1.id)
  
      class Meta: #
          ordering = ['-field3']
  
      def insert_history(self):
          # Here I will have some business logic to insert the data to the history model
  </code></pre>
  
  <p>To insert data to the History model I will allways have to use the "insert_history" function. </p>
  
  <p>My questions here are:</p>
  
  <p><strong>The code above is correct?</strong> </p>
  
  <p><strong>If yes, how can I call the "insert_history" from a view?</strong></p>
  
  <hr>
  
  <p><strong>Alternative 2</strong></p>
  
  <p>I've another alternative that I've tested and it works, but does not feel the right way. The code looks like this:</p>
  
  <pre><code>class History(models.Model):
      field1 = models.ForeignKey(Request)
      field2 = models.BooleanField()
      field3 = models.DateTimeField()
  
      def __unicode__(self):
          return str(self.field1.id)
  
      class Meta: #
          ordering = ['-field3']
  
  def insert_history(field1, field2, field3):
      # Here I will have some business logic to insert the data to the history model
  </code></pre>
  
  <p>And I call it from a view like this:</p>
  
  <pre><code>from app.models import insert_history
  
  insert_history('1', True, 'some_date')
  </code></pre>
  
  <p>what is the correct way of doing it? If the alternative 1 is correct, how can I call the "insert_history" from a view?</p>
  
  <p>Best Regards,</p>