Created
July 20, 2016 19:21
-
-
Save solanoize/a694683caf2612cbd2ca2936adbb716a to your computer and use it in GitHub Desktop.
Problematika masalah Sub Query untuk merekap data absensi dengan Django ORM (QuerySet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Thanks a lot for Mr. Adiyat Mubarok | |
| # for solution about my problem :) | |
| # from models | |
| # open https://gist.github.com/yanwarsolah/e72e0a013b36142cdc89765cdd011de6 | |
| # type following code in django python shell (python manage.py shell) | |
| from django.db.models import CharField, Case, Value, When | |
| from django.db.models import Count | |
| from absensi_sma.models import * | |
| from pprint import pprint as p | |
| data_obj = Absens.objects.filter(semester__status='Y', nis__kelas__kelas='X')\ | |
| .values('nis__nama', 'nis__nis')\ | |
| .annotate( | |
| izin=Count(Case(When(absen='I', then=1), output_field=1)), | |
| alpha=Count(Case(When(absen='A', then=1), output_field=1)), | |
| sakit=Count(Case(When(absen='S', then=1), output_field=1)), | |
| masuk=Count(Case(When(absen='M', then=1), output_field=1)) | |
| ) | |
| print(data_obj) | |
| for data in data_obj: | |
| p(data) | |
| """ | |
| print(data_obj) | |
| for data in data_obj: | |
| print(data) | |
| result: | |
| {'alpha': 0, | |
| 'izin': 0, | |
| 'masuk': 2, | |
| 'nis__nama': 'Eni Alan Smith', | |
| 'nis__nis': 'N002', | |
| 'sakit': 0} | |
| {'alpha': 1, | |
| 'izin': 0, | |
| 'masuk': 1, | |
| 'nis__nama': 'Hanifah Andriyani', | |
| 'nis__nis': 'N001', | |
| 'sakit': 0} | |
| {'alpha': 0, | |
| 'izin': 0, | |
| 'masuk': 1, | |
| 'nis__nama': 'Olip Astuti', | |
| 'nis__nis': 'N003', | |
| 'sakit': 0} | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment