Enter admin as a Facebook user in Django's social_auth

Asked 1 years ago, Updated 1 years ago, 95 views

I use the django-social-auth plug-in in Django.

I would like to join Django's admin as a user generated after I authenticate with Facebook.

First, create this user on models.py and
To force is_staff to be true, we did the following:

class CustomUser (User):
    class Meta:
        verbose_name = u 'Custom User'
        verbose_name_plural= verbose_name

    def__init__(self, *args, **kwargs):
        super(User,self).__init__(*args,**kwargs)
        # self._meta.get_field('is_staff') .default=True #Tried but failed
        super(User,self)._meta.get_field('is_staff') .default=True

# CustomUser._meta.get_field('is_staff').default=True

at settings.py
SOCIAL_AUTH_USER_MODEL='my_app.CustomUser' 

It was done as shown in .
I've tried a lot of comments.

However, I was unable to log in as a Facebook user.

Next, I found a similar article.

https://djangosnippets.org/snippets/2856/

But I couldn't.

on settings.py USE_SOCIAL_AUTH_AS_ADMIN_LOGIN, but SOCIAL_AUTH_USE_AS_ADMIN_LOGIN on admin.py I thought it was strange, so I changed it to the same variable, but I still couldn't.

If there is a solution, please let me know.

python oauth social-framework facebook-api django

2022-09-29 22:09

1 Answers

However, I was unable to log in as a Facebook user.

I think this depends on the user being created on the Django side.
So,

Trying to force is_staff to be true

I will only answer this part.The following are the CUTOMUser improvement codes:

class CustomUser (User):
    class Meta:
        verbose_name = u 'Custom User'
        verbose_name_plural= verbose_name

    def__init__(self, *args, **kwargs):
        super(User,self).__init__(*args,**kwargs)
        self.is_staff=True 

Since is_staff is already false in the first process of _init__(), we have unconditionally set it to True in the following process:


2022-09-29 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.