Mysql query in django view
<p>I do not have much knowledge of mysql. My django model is as follows: </p>

<pre><code> class Exercise_state(models.Model):
     exercise = models.ForeignKey(Exercise, blank=True, null=True)
     user = models.ForeignKey(User, blank=True, null=True) 
     intensity_level = models.IntegerField(default='1')
     progress = models.IntegerField(default='0')
     current_date = models.DateField(auto_now=True)
     user_rating = models.IntegerField(default='0')
</code></pre>

<p>I want to retrieve count of user_id's which satisfy exercise_id=1 corresponding intensity_level=7 and so on for all exercise_id's till 7. In short, users who have reached intensity_level=7 in all the exercises. </p>

<p>I have written one query which is as follows:</p>

<pre><code>select count(user_id) from demo_exercise_state where 
exercise_id=1 and intensity_level=7 and
exercise_id=2 and intensity_level=7 and
exercise_id=3 and intensity_level=7 and 
exercise_id=4 and intensity_level=7 and
exercise_id=5 and intensity_level=7 and
exercise_id=6 and intensity_level=7 and
exercise_id=7 and intensity_level=7;
</code></pre>

<p>As far as I can cross check results from my database directly no user has yet completed his training (i.e. who has reached intensity_level=7 in all exercise types), so it returns the count as '0'. </p>

<p>I am sure this can query be optimized as there is quite much repeatation, but I am not sure how.</p>

<p>Also I want to execute the same (i.e. get the same result) in my django view. There I have something like</p>

<pre><code>all_exercise_finish_count = Exercise_state.objects.filter(exercise_id=1, intensity_level=7).count()
</code></pre>

<p>How can I reflect the same in this django view?</p>