Django view fetching data from two models in a single query -


i have two tables in database relevant problem:

exercise_state following fields:

| id | intensity_level | progress | exercise_id | user_id | current_date | user_rating | 

auth_user following fields:

| id | password | last_login | is_superuser | username | first_name | last_name | email | is_staff | is_active | date_joined | 

right fetching data in view follows:

def get_specific_exercise_finish_count(request, exerciseid):     # number of users completed specific exercise     specific_exercise_finish_count = exercise_state.objects.filter(exercise_id=exerciseid, intensity_level=7).count()      data = {}     data['count'] = specific_exercise_finish_count      return jsonresponse(data) 

now want filter results further specific set of usernames i.e. usernames starts 'yg_' (i have 2 sets of usernames registered in system 1 group starts 'yg' , other 'yg_'). username not field of exercise_state, not sure how proceed.

how can achieve this?

i solved following code of piece, see part in filter statement:

def get_specific_exercise_finish_count_memorygames(request, exerciseid):     # number of users completed specific exercise     specific_exercise_finish_count_memorygames = exercise_state.objects.filter(exercise_id=exerciseid, intensity_level=7, user__username__startswith='yg_').count()      data = {}     data['count'] = specific_exercise_finish_count_memorygames      return jsonresponse(data) 

Comments