title: Check if model field exists in Django
body: <p>This is the solution I came up, in my models.py:</p>
  
  <pre><code>from django.db import models
  
  @classmethod
  def model_field_exists(cls, field):
      try:
          cls._meta.get_field(field)
          return True
      except models.FieldDoesNotExist:
          return False
  
  models.Model.field_exists = model_field_exists
  </code></pre>
  
  <p>And use it like:</p>
  
  <pre><code>Post.field_exists('title') # &gt; True or False
  </code></pre>
  
  <p>The problem comes with foreign keys, my Post model belongs to a Category, this check works:</p>
  
  <pre><code>Post.field_exists('category') # &gt; True
  </code></pre>
  
  <p>But this one doesn't:</p>
  
  <pre><code>Post.field_exists('category_id') # &gt; False
  </code></pre>
  
  <p>This is the actual field name in db and I need to check for it like this. How can I do it in django?</p>