관리자를 사용할 수 없습니다. 사용자가 'pet.Person'으로 교체되었습니다.
나는 django에서 기본 사용자 모델을 꽤 오랫동안 사용하고 있으며, 더 향상시켜야 할 필요가 있다면 django 1.5에서 나만의 사용자 정의 사용자 모델을 만들어야한다는 것을 깨달았습니다.
사용자 지정 사용자 모델을 만들었고 사용자가 로그인 할 수있는 기능이 있습니다. 내 사용자 지정 사용자 모델이 request.user를 허용하지 않기 때문에 내 기능과 호환되지 않는다고 생각합니다. request.user를 다시 사용할 수 있도록이 문제를 어떻게 해결할 수 있습니까?
견해
def LoginRequest(request):
form = LoginForm(request.POST or None)
if request.user.is_authenticated():
username = User.objects.get(username=request.user)
url = reverse('world:Profile', kwargs = {'slug': person.slug})
return HttpResponseRedirect(url)
if request.POST and form.is_valid():
user = form.authenticate_user()
login(request, user)
username= User.objects.get(username=request.user)
person = Person.objects.get(user=request.user)
url = reverse('world:Profile', kwargs = {'slug': person.slug})
return HttpResponseRedirect(url)
return render(request, 'login.html',{'form': form})
모델
class PersonManager(BaseUserManager):
def create_user(self, email,date_of_birth, username,password=None,):
if not email:
msg = 'Users must have an email address'
raise ValueError(msg)
if not username:
msg = 'This username is not valid'
raise ValueError(msg)
if not date_of_birth:
msg = 'Please Verify Your DOB'
raise ValueError(msg)
user = self.model(
email=PersonManager.normalize_email(email),username=username,date_of_birth=date_of_birth)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self,email,username,password,date_of_birth):
user = self.create_user(email,password=password,username=username,date_of_birth=date_of_birth)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class Person(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name='email address',max_length=255,unique=True,db_index=True,)
username = models.CharField(max_length=255, unique=True)
date_of_birth = models.DateField()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'date_of_birth',]
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
objects = PersonManager()
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def __unicode__(self):
return self.email
문제는 그것이 User
참조 django.contrib.auth.models.User
하고 이제 당신은 Custom User
pet.Person
당신이settings.py
AUTH_USER_MODEL = "pet.Person"
당신은 정의해야 User
와 Custom User
모델 그리고 당신은이 작업을 수행 할 수 있습니다 get_user_model
사용하는 파일의 맨 위에User
from django.contrib.auth import get_user_model
User = get_user_model()
now you will be able to use Custom User
model and the problem has been fixed.
For anyone else who might come across this problem, I also solved it by simply doing this on forms.py:
add this at the top of the forms.py file
from .models import YourCustomUser
and then add this to your forms.py CustomUser
form:
class SignUpForm(UserCreationForm):
#profile_year = blaaa blaa blaaa irrelevant.. You have your own stuff here don't worry about it
# here is the important part.. add a class Meta-
class Meta:
model = YourCustomUser #this is the "YourCustomUser" that you imported at the top of the file
fields = ('username', 'password1', 'password2', #etc etc, other fields you want displayed on the form)
BIG NOTES, ATTENTION:
This code worked for my case. I have a view for signing users up, I had a problem here and I solved it, I haven't tried it for logging in users.
The
include = ()
part is required, or you can addexclude = ()
, but you have to have one
Important caveat to update the above solutions... If you're facing this kind of problem, you've probably tried various solutions around the web telling you to add AUTH_USER_MODEL = users.CustomUser
to settings.py
and then to add the following code to views.py
forms.py
and any other file that calls User
:
from django.contrib.auth import get_user_model
User = get_user_model()
And then you scratch your head when you get the error:
Manager isn't available; 'auth.User' has been swapped for 'users.User'
Anytime your code references User
such as:
User.objects.get()
Cause you know you already put objects = UserManager()
in your custom user class (UserManager
being the name of your custom manager that extends BaseUserManager
).
Well as it turns out doing:
User = get_user_model() # somewhere at the top of your .py file
# followed by
User.objects.get() # in a function/method of that same file
Is NOT equivalent to:
get_user_model().objects.get() # without the need for User = get_user_model() anywhere
Perhaps not intuitive, but it turns out that that in python, executing User = get_user_model()
once at the time of import does not then result in User
being defined across subsequent calls (i.e. it does not turn User
into a "constant" of sorts which you might expect if you're coming from a C/C++ background; meaning that the execution of User = get_user_model()
occurs at the time of imports, but is then de-referenced before subsequent called to class or function/method in that file).
So to sum up, in all files that reference the User
class (e.g. calling functions or variables such as User.objects.get()
User.objects.all()
User.DoesNotExist
etc...):
# Add the following import line
from django.contrib.auth import get_user_model
# Replace all references to User with get_user_model() such as...
user = get_user_model().objects.get(pk=uid)
# instead of user = User.objects.get(pk=uid)
# or
queryset = get_user_model().objects.all()
# instead of queryset = User.objects.all()
# etc...
Hope this helps save others some time...
'UFO ET IT' 카테고리의 다른 글
Heroku H18 오류를 어떻게 해석해야합니까? (0) | 2020.12.03 |
---|---|
jQuery.attr () 함수를 사용하여 여러 데이터 속성을 설정할 수 있습니까? (0) | 2020.12.03 |
SQL 명령에 대한 명령 시간 초과 늘리기 (0) | 2020.12.03 |
AngularJs 양식의 첫 번째 잘못된 입력에 초점 설정 (0) | 2020.12.03 |
iOS Swift를 사용한 NSURLConnection (0) | 2020.12.03 |