5👍
View functions get request
as one of their parameters. Assuming you’re using the built in auth app, the current user (if any) will be request.user
. This may be a representation of an anonymous user if the session does not have a logged-in user – see the docs if you don’t want to allow that.
Users can be in many groups, so I’m not quite sure what you mean by “group id”. The user’s groups are available as a standard many-to-many field, as request.user.groups
. That’s a manager object, so you can apply filters and such to it – to get them all, request.user.groups.all()
.
6👍
user = auth.get_user(request)
group = request.user.groups.values_list('name', flat=True).first()
Works Perfect!
So u can replace ‘name’ with ‘id’ to get group’s ID
- [Django]-DRF APIView – How can I add help text to auto generated form?
- [Django]-Using reverse relationships with django-rest-framework's serializer
- [Django]-Try/except database query in django
0👍
If there is one and only one group per user in your use case, you can get that group by using request.user.groups.get()
. objects.get()
works somewhat similar to objects.all()[0]
. So it will fail when there are more than one group for that user.
https://github.com/django/django/blob/master/django/db/models/query.py
def get(self, *args, **kwargs):
"""
Performs the query and returns a single object matching the given
keyword arguments.
"""
clone = self.filter(*args, **kwargs)
if self.query.can_filter():
clone = clone.order_by()
clone = clone[:MAX_GET_RESULTS + 1]
num = len(clone)
if num == 1:
return clone._result_cache[0]
if not num:
raise self.model.DoesNotExist(
"%s matching query does not exist." %
self.model._meta.object_name)
raise self.model.MultipleObjectsReturned(
"get() returned more than one %s -- it returned %s!" % (
self.model._meta.object_name,
num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS
)
)
If you are after categorising your users, just extend the user model and set a foreign key field. Using permission groups for that matter doesn’t seem like a good approach to me.
- [Django]-Django-mptt filter without breaking the tree
- [Django]-Running Django 1.5 and Django 1.3 on the same server
- [Django]-CodeIgniter authentication, permissions and admin system, or any other PHP equivilant of Django?
- [Django]-How to return most popular items in a table, but where each item is unique?
0👍
If you debug with the break point on the method below,
@csrf_exempt
def auth_check(request):
return HttpResponse(str(request.user)) # <- breakpoint
you can see that there are user.groups
in the request.
As shown in the highlighted part above, you can find the groups name to which the user belongs.
you can easily get user group id
request.user.groups.name
- [Django]-Missing staticfiles manifest entry in Django deployment using Heroku
- [Django]-When does the queryset use its _result_cache, not hitting the database?
- [Django]-How to add custom user fields of dj_rest_auth package
- [Django]-Django replaces non-ascii characters with \ufffd