4747

How to find User queryset matching self.request.user

Question:

How do we find User queryset matching self.request.user?

logged_in_user = User.objects.filter(id=self.request.user.id)

I wish there is much efficient way to doing this. (such as get_user_model(self.request.user)) ?

Here is my views.py

class ProfilePageView(generics.RetrieveAPIView): serializer_class = ProfilePageSerializer def get_queryset(self): logged_in_user = User.objects.filter(username=self.request.user.username) << right here! return logged_in_user def get_object(self): queryset = self.get_queryset() obj = get_object_or_404(queryset) return obj

Answer1:

There is no reason to get a queryset with just the one User instance in it, when you already have the user instance:

logged_in_user = self.request.user

Answer2:

may be it help you?

def get_object(self): return self.request.user

Recommend

  • Why am I be getting a 403 Forbidden error when using @csrf_exempt in AJAX request?
  • DeleteView with 2 arguements post and user
  • query.group_by in Django 1.9
  • Python assertRaises on user-defined exceptions
  • Django pagination with next, previous, count
  • Django Celery - How to start a task with a delay of n - seconds - countdown flag is ignored
  • Django RESTful API error “type object 'User' has no attribute '_meta'”
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Django model for a Postgres view
  • Do query loads all the data in memory
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Typescript - Unable to get 'import' statement to function
  • CSS Linear-gradient formatting issue accross different browsers
  • Spring security and special characters
  • Obtain ObjectIdHex value from mgo query
  • How to check if every primary key value is being referenced as foreign key in another table
  • ILMerge & Keep Assembly Name
  • Javascript convert timezone issue
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Why winpcap requires both .lib and .dll to run?
  • Traverse Array and Display in markup
  • retrieve vertices with no linked edge in arangodb
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • How to stop GridView from loading again when I press back button?
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Busy indicator not showing up in wpf window [duplicate]
  • How can i traverse a binary tree from right to left in java?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF