Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:44:07+00:00 2026-06-01T02:44:07+00:00

I have the following user resource: class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name

  • 0

I have the following user resource:

class UserResource(ModelResource):
  class Meta:
    queryset = User.objects.all()
    resource_name = 'user'
    fields = ['username', 'first_name', 'last_name']
    allowed_methods = ['get']
    filtering = {
      'username': ALL,
      'id': ALL,
    }

and the following model resource:

class GoalResource(ModelResource):
  user = fields.ForeignKey(UserResource, 'user')

  class Meta:
    #authentication = BasicAuthentication()
    #authorization = ReadOnlyAuthorization()
    queryset = Goal.objects.all()
    resource_name = 'goal'
    filtering = {
      'user': ALL_WITH_RELATIONS,
    }

I want to be able to filter the goal by user id rather than username.

I can get a list of goals from certain usernames by doing a GET request on this:

http://localhost:8000/api/v1/goal/?user__username=test

But I want to be able to sort by user id instead:

http://localhost:8000/api/v1/goal/?user__id=1

How would I get the second part to work?

Also, what is the general procedure for accessing a currently logged in user’s id through Javascript? I am using backbonejs, and I want to do a post for all of a logged in user’s goal. I thought about putting a hidden field on the page with the user’s id. Then extracting the value of the hidden field from the DOM, but I figured it’s easy to use chrome’s developer tools to change the id whenever I want. Of course, I will use authentication to check that the logged in user’s id matches the one that I extract from the hidden field, though. But what is the accepted way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:44:08+00:00Added an answer on June 1, 2026 at 2:44 am

    I am not sure if what I propose here can work in your authorization. It works for me using ApiKeyAuthorization and Authorization.

    I read the idea from:
    http://django-tastypie.readthedocs.org/en/latest/cookbook.html [Section: Creating per-user resources ]

    My suggestion is:

    What about uncommenting authentication and authorization, and overriding obj_create and apply_authorization. I am using that in my project, and it works. In the code of the method apply_authorization, I just added the if condition checking for superuser, you can just return the object_list+filter without checking that (I do it cause if is not superuser, I return data related to groups of users).

    class GoalResource(ModelResource):
      user = fields.ForeignKey(UserResource, 'user')
    
      class Meta:
        authentication = BasicAuthentication()
        authorization = ReadOnlyAuthorization()
        queryset = Goal.objects.all()
        resource_name = 'goal'
        filtering = {
          'user': ALL_WITH_RELATIONS,
        }
    
       def obj_create(self, bundle, request=None, **kwargs):
           return super(EnvironmentResource, self).obj_create(bundle, request, user=request.user)
    
    
       def apply_authorization_limits(self, request, object_list):
           if request.user.is_superuser:
               return object_list.filter(user__id=request.GET.get('user__id',''))
    

    Hope is what you were asking, and it helps.
    best with that!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have the following model: class User: name = models.CharField( max_length = 200 )
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have the following django model: RESOURCE_DIR = os.path.join(settings.MEDIA_ROOT, 'resources') class Resource(models.Model): title =
I have the following user control that is embedded within another user control. <UserControl.Resources>
I have the following user-control: <%@ Control Language=C# AutoEventWireup=true CodeFile=FadingMessage.ascx.cs Inherits=includes_FadingMessage %> <asp:PlaceHolder Visible=false
So I have the following user defined type in my oracle database: CREATE OR
I have the following domains: User, Role, Company. User and Role has m:n relationship,
I have the following situation: A user will define a certain filter on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.