我的数据库中有两个表与此问题相关:
exercise_state ,包含以下字段:
| id | intensity_level | progress | exercise_id | user_id | current_date | user_rating |
auth_user ,包含以下字段:
| id | password | last_login | is_superuser | username | first_name | last_name | email | is_staff | is_active | date_joined |
现在我在我的视图中提取一些数据如下:
def get_specific_exercise_finish_count(request, exerciseId):
# Number of users who completed a 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)
现在我想针对特定的用户名集进一步过滤这些结果,即以'yg_'开头的用户名(我在我的系统中注册了两组用户名,一组用'yg'开头,另一组用'yg_'开头) 。由于用户名不是 exercise_state 的字段,我不知道如何继续。
我怎样才能做到这一点?
答案 0 :(得分:0)
我用下面的代码解决了它,请参阅filter语句中的额外部分:
def get_specific_exercise_finish_count_memoryGames(request, exerciseId):
# Number of users who completed a 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)