title: Count the failed login attemps and lockout user in Django
body: <p>I am trying to count the number of invalid login attempts using "user_login_failed_callback" method. As shown the code below, I am trying to access previous database field value of "failed_login_count" from "user_login_failed_callback" method. I will update the value of failed_login_count to failed_login_count+1 and also update the date and time to current time.</p>
  
  <p>Currently I am accessing "failed_login_count" from "user.get_profile()". I know which is wrong. Could you please point the right way to access BruteForceEntry model fields failed_login_count.</p>
  
  <p>My questions are</p>
  
  <ol>
  <li><p>So could you please let me know how can I access failed_login_count from database and update the existing value.</p></li>
  <li><p>As shown in below code I am using "BruteForceEntry.objects.filter" to update existing database row. So is this the correct way to update table.</p></li>
  <li><p>How can I lock out user account after more than 10 failed login attemps.</p></li>
  </ol>
  
  <p>Could you please help me.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class BruteForceEntry(models.Model):
  
      user = models.ForeignKey(User, unique=True)
      username = models.CharField(max_length=256, null=True)
      data_time_of_last_unsuccessful_login = models.DateTimeField(auto_now=True, blank=True)
      failed_login_count = models.IntegerField(default=0)
  
      def __unicode__(self):
          return '{0} - {1} - {2}'.format(self.username, self.data_time_of_last_unsuccessful_login, self.failed_login_count)
  
      def __str__(self):
          return '{0} - {1} - {2}'.format(self.username, self.data_time_of_last_unsuccessful_login, self.failed_login_count)
  
  @receiver(user_login_failed)
  def user_login_failed_callback(sender, credentials, **kwargs):
      username = credentials['username']
      user = User.objects.get(username=username)
      profile = user.get_profile()
      failed_login_count = profile.failed_login_count + 1
  
      BruteForceEntry.objects.filter(username=username).update(data_time_of_last_unsuccessful_login=timezone.now(), failed_login_count=failed_login_count)
      </code></pre>
  </div>
  </div>
  </p>