group by django model code example
Example 1: django group by
from django.db.models import Count
Members.objects.values('designation').annotate(dcount=Count('designation'))
SELECT designation, COUNT(designation) AS dcount
FROM members GROUP BY designation
[{'designation': 'Salesman', 'dcount': 2},
{'designation': 'Manager', 'dcount': 2}]
Example 2: create django group
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from api.models import Project
new_group, created = Group.objects.get_or_create(name='new_group')
ct = ContentType.objects.get_for_model(Project)
permission = Permission.objects.create(codename='can_add_project',
name='Can add project',
content_type=ct)
new_group.permissions.add(permission)