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 8319729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:24:52+00:00 2026-06-08T22:24:52+00:00

I’m trying to override is_authenticated in my custom authentication. I have something simple (to

  • 0

I’m trying to override is_authenticated in my custom authentication. I have something simple (to start with) like this:

class MyAuthentication(BasicAuthentication):
    def __init__(self, *args, **kwargs):
        super(MyAuthentication, self).__init__(*args, **kwargs)

    def is_authenticated(self, request, **kwargs):
        return True

then in my ModelResource I have

class LoginUserResource(ModelResource):

    class Meta:
        resource_name = 'login'
        queryset = User.objects.all()
        excludes = ['id', 'email', 'password', 'is_staff', 'is_superuser']
        list_allowed_methods = ['post']

        authentication = MyAuthentication()
        authorization = DjangoAuthorization()

I keep getting a 500 error back with "error_message": "column username is not unique". I only have one username in the db and it’s the user I am trying to authenticate.

Any ideas as to why it’s returning this error? How would I allow an api client to login?

Thanks for the help.

  • 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-08T22:24:55+00:00Added an answer on June 8, 2026 at 10:24 pm

    Your approach will try to create a new user with the username that you are authenticating with. This will bubble up at the DB layer, as you’ve noticed, that such a user already exists.

    What you want is to create a UserResource, add a method on it that users can post to and login with data passing in username/password.

    from django.contrib.auth.models import User
    from django.contrib.auth import authenticate, login, logout
    from tastypie.http import HttpUnauthorized, HttpForbidden
    from django.conf.urls import url
    from tastypie.utils import trailing_slash
    
    class UserResource(ModelResource):
        class Meta:
            queryset = User.objects.all()
            fields = ['first_name', 'last_name', 'email']
            allowed_methods = ['get', 'post']
            resource_name = 'user'
    
        def override_urls(self):
            return [
                url(r"^(?P<resource_name>%s)/login%s$" %
                    (self._meta.resource_name, trailing_slash()),
                    self.wrap_view('login'), name="api_login"),
                url(r'^(?P<resource_name>%s)/logout%s$' %
                    (self._meta.resource_name, trailing_slash()),
                    self.wrap_view('logout'), name='api_logout'),
            ]
    
        def login(self, request, **kwargs):
            self.method_check(request, allowed=['post'])
    
            data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
    
            username = data.get('username', '')
            password = data.get('password', '')
    
            user = authenticate(username=username, password=password)
            if user:
                if user.is_active:
                    login(request, user)
                    return self.create_response(request, {
                        'success': True
                    })
                else:
                    return self.create_response(request, {
                        'success': False,
                        'reason': 'disabled',
                        }, HttpForbidden )
            else:
                return self.create_response(request, {
                    'success': False,
                    'reason': 'incorrect',
                    }, HttpUnauthorized )
    
        def logout(self, request, **kwargs):
            self.method_check(request, allowed=['get'])
            if request.user and request.user.is_authenticated():
                logout(request)
                return self.create_response(request, { 'success': True })
            else:
                return self.create_response(request, { 'success': False }, HttpUnauthorized)
    

    Now you can do send a POST to http://hostname/api/user/login with data
    { 'username' : 'me', 'password' : 'l33t' }.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

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.