I'm using userena 1.2 and Django 1.4. I'm trying to create multiple user profiles but I had no luck following many post found here on SO.
I've create 2 custom Models and a form like so:
class BaseProfile(UserenaBaseProfile):
""" Default profile """
user = models.OneToOneField(User, unique=True, verbose_name = _('user'), related_name='base_profile')
#property # accessing a method as a property
def is_seller(self):
"""Find out if this is a Seller user"""
try:
self.seller
return True
except Seller.DoesNotExist:
return False
def get_profile_type(self):
"""return the profile type model"""
if self.is_seller:
return self.seller
else:
return self.customer
class Seller(BaseProfile):
company = models.CharField(_('Company'),max_length=100, null=True, blank=True,)
class Customer(BaseProfile):
favourite_snack = models.CharField( _('favourite snack'), max_length=5 )
And overridden the Signup form:
class SignupFormExtra(SignupForm):
# stuff in here
def save(self):
# stuff in here
Now the problem is inside the save method.
Based on this answer I've tried to implement a custom manager but I had no luck (I'm a django newbie). The important point that I understood is that the Save method shoud return a Django User, not a userena profile.
def save(self):
user = super(SpamSignupForm,self).save()
new_customer = Customer.objects.create_user()
return new_user
Then I've tried something like this:
def save(self):
new_user = super(SignupFormExtra, self).save()
new_user.save()
customer = Customer(profile = new_user.get_profile(), user=new_user)
customer.save()
return new_user
The get_profile() method will always (I guess) returns the base profile defined in settings.AUTH_PROFILE_MODULE.
Also it seems wrong to me that the author used a profile field in the subprofile as a OneToOne relation to the Baseprofile WHILE implementing a multi-table inheritance, why? It doesn't seem right to me.
class Customer(Baseprofile):
profile = models.OneToOneField(Baseprofile,
unique=True,
verbose_name=_('profile'),
related_name='student')
Yes, basically I've spent a full day trying to figure this out and I'm still lost.
it is better to use django-registration. install and add it to INSTALLED_APPS =(.....'registration',
now create two user in
registration/models.py
.................
class Seller(models.Model):
user=models.OneToOneField(User)
companyname= models.CharField(max_length=100,blank=True)
def __unicode__(self):
return self.user.username
class Customer(models.Model):
user=models.OneToOneField(User)
birth_date = models.DateField(null=True, blank=True)
favourite_snack = models.CharField(max_length=5)
def __unicode__(self):
return self.user.username
in registration/views.py
........
from django.contrib.auth import authenticate, login
from registration.models import Seller,Customer
from django.http import HttpResponse
def auth_login(request,utype):
temp_login='registration/%slogin.html' %utype
try:
username = request.POST['username']
password = request.POST['password']
except KeyError:
return render(request,temp_login)
user = authenticate(username=username, password=password)
if utype=='seller':
try:Seller.objects.get(user=user)
except: return render(request,temp_login,{'errors':True})
if utype=='customer':
try:Customer.objects.get(user=user)
except: return render(request,temp_login,{'errors':True})
if user.is_active:
login(request,user)
return HttpResponseRedirect('/'+request.GET['next'])#,render(request,'%s/home.html' %utype)
return render(request,temp_login,{'errors':True})
edit registration/auth_urls.py
urlpatterns = patterns('',
url(r'^login/(employer|jobseeker)/$',
auth_login,name='auth_login'),
registration/backends/default/views.py
inside class RegistrationView(BaseRegistrationView):
inside def register(self, request, **cleaned_data): add
if self.args[0]=='e': usertype=Seller()
else: usertype=Customer()
usertype.user=new_user
usertype.save()
registration/backends/default/urls.py
add these line
url(r'^register/([ej])/$',
RegistrationView.as_view(),
name='registration_register'),
Related
So i am stuck at a point in my project. I am trying to create users and I have extended User model using the OneToOnefield method as recommended in Documentation to create a user profile. There is no problem in creation of the user as all the details are correctly stored in both the auth_user and appname_userprofile table.
The problem I get is when I try to login with any user stored in the auth_user table. Except one case( which i created before creating the userProfile model). So in my auth_user Table the pass for this one case is encoded but for the remaining cases it's plain text.
Here is my function handling the view -
def login_view(request):
if request.method == 'POST':
print(request.body)
username = request.POST.get('id_username')
password = request.POST.get('id_password')
print(username, password)
try:
import pdb; pdb.set_trace()
user = authenticate(username=username, password=password)
print('This is ',user)
if user is not None:
login(request,user)
else:
return redirect('fileupload')
print('This is ',user).... some more code
Now when i pass the credentials for this one case and try to authenticate. the login works(except my code gets stuck at some later point because this user doesn't have any profile).
When i pass the credentials for user created after creating the UserProfile model. The
authenticate(username=username, password=password)
function returns
None
I have included this in my settings.py -
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
This is my model -
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
contact_no = models.CharField(max_length=10, null=True)
# email = models.EmailField()
department = models.CharField(max_length=25, null=True)
status = models.IntegerField(null=True)
industry_segment = models.CharField(max_length=50, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.IntegerField(null=True)
updated_at = models.DateTimeField(auto_now=True)
updated_by = models.IntegerField(null=True)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
This is how i am saving the users -
def user_view(request):
print(request.user)
if request.is_ajax():
print(request.user)
data = json.load(request)
u_name = data['txtfirstName'] + " " + data['txtlastName']
user=User(username=u_name,password='test123',email=data['txtemailId'])
user.save()
# userprofile= UserProfile.objects.create(user=request.user)
user.userprofile.contact_no = data['txtcontactNo']
# user.userprofile.email = data['txtemailId']
user.userprofile.department = data['txtdeptvalue']
user.userprofile.status = data['txtstatusvalue']
user.userprofile.industry_segment = 'Software'
user.userprofile.created_at = datetime.datetime.now()
user.userprofile.created_by = request.user.id
user.userprofile.updated_at = datetime.datetime.now()
user.userprofile.updated_by = request.user.id
user.save()
ignore the formatting of this code... but it is working
So can anyone help me with the authentication problem ?
You must not create users like that. The password will not be hashed, so will never match in authenticate. You must always use the create_user manager method.
user = User.objects.create_user(username=u_name, password='test123', email=data['txtemailId'])
I'm been using the default user model in django for quite a abit and I realize , if I need to further enhance it , I would have to create my own custom User Model in django 1.5 .
I created my custom user model and I have a function which allows users to sign in .
I think my custom user model is incompatible with my function because it wouldn't allow me to do request.user . How can I fix this so I can use request.user again?
views
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})
models
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
The problem is that User refers to django.contrib.auth.models.User and now you have got a Custom User pet.Person assuming you have in the settings.py
AUTH_USER_MODEL = "pet.Person"
you have to define User with the Custom User model and you can do this with get_user_model at the top of the file where you use 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 add exclude = (), 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...
In forms.py
# change
from django.contrib.auth.models import User
# to
from django.contrib.auth import get_user_model
Then add the following code at the top
User = get_user_model()
All the solutions provided above did not work in my case. If you using Django version 3.1 there is another solution for you:
In auth/forms, comment out line 10 and change the model in line 104 & 153 to your defined model.
django-registration 1.0 now has support for django 1.5 custom user models. The django-registration documentation only has the following FAQ item about it:
I’m using Django 1.5 and a custom user model; how do I make that work?
Although the two built-in backends supplied with
django-registration both assume Django’s default User model, the base
view classes are deliberately user-model-agnostic. Simply subclass
them, and implement logic for your custom user model.
I'm not sure which views I need to subclass and what should go in them. I've also noticed that the ProfileManager in django-registration still assumes a separate username field.
In my specific case, I've removed the 'username' field, added a 'display_name', and made 'email' the identifying field:
class MyUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name="Email Address",
max_length=384,
unique=True,
db_index=True,)
display_name = models.CharField(max_length=128, blank=True)
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def __unicode__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
#property
def is_staff(self):
return self.is_admin
Without subclassing any django-registration classes, the default rendering of the registration form pulls in fields from User instead of MyUser.
I've seen the following SO thread django-registration app and Django 1.5 custom user model, but it didn't help.
Update
I've noticed that RegistrationForm is hardcoded with a 'username' field. The FAQ only mentions subclassing the backend, so I'm not sure what the intention is here. Should I subclass the form as well?
Some parts are most definitely not Django 1.5 compatible yet: https://bitbucket.org/ubernostrum/django-registration/src/8f242e35ef7c004e035e54b4bb093c32bf77c29f/registration/forms.py?at=default#cl-48
class RegistrationForm(forms.Form):
# ...
def clean_username(self):
# ...
# The line below needs fixing
existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
if existing.exists():
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['username']
So unless those methods are changed and/or you subclass them, it won't work yet.
For your specific case this registration form should do the trick:
from registration import forms as registration_forms
from django.contrib import auth
class RegistrationForm(registration_forms.RegistrationForm):
def clean_username(self):
'''
Validate that the username is alphanumeric and is not already
in use.
'''
User = auth.get_user_model()
existing = User.objects.filter(display_name__iexact=self.cleaned_data['username'])
if existing.exists():
raise forms.ValidationError(_("A user with that name already exists."))
else:
return self.cleaned_data['username']
In addition to a custom property on your model:
class MyUser(AbstractBaseUser, PermissionsMixin):
# ...
def get_username(self):
return self.display_name
def set_username(self, username):
self.display_name = username
def del_username(self):
del self.display_name
username = property(get_username, set_username, del_username)
I have read this and I have one question. How to take access to the 'other fields'?
models:
class UsersProfile(models.Model):
def __unicode__(self):
return self.user.last_name
user = models.OneToOneField(User)
car_num = models.IntegerField('car ID')
captain = models.BooleanField()
views:
#login_required
def profile(request):
return render(request, 'profile.html')
So, after authentication, in profile works only user.last_login + etc, but not user.car_num or user.captain. I read about get_profile(), but where I need it to write?
profile = request.user.get_profile()
If it in views, how to return?
You can define a related_name for your OneToOneField, like this:
user = models.OneToOneField(User, related_name='profile')
and then access the corresponding fields using
request.user.profile.car_num
I'm creating a custom profile page and I need the user to be able to save or update his profile. For some reason I'm getting the following error when I hit the update profile button:
User matching query does not exist.
Below is the code I have for the forms, models and views files:
forms.py
class ProfileForm(forms.Form):
profile_name = forms.CharField( max_length=50)
models.py
class UserProfile(models.Model):
user = models.ForeignKey(User)
profile_name = models.CharField(blank=True, max_length=50)
def __unicode__(self):
return u'%s %s' % (self.user, self.profile_name)
views.py
def edit_profile(request):
if 'edit_button' in request.POST:
form = ProfileForm(request.POST)
f_user = User.objects.get(username=request.user.id)
f_profile_name = form.cleaned_data['profile_name']
p = UserProfile(user=f_user, profile_name=f_profile_name)
p.save()
else:
form = ProfileForm()
return render_to_response('userprofile_template.html', locals(), context_instance=RequestContext(request))
Any idea what am I not doing right? Thank You!
f_user = User.objects.get(username=request.user.id)
Username is presumabely request.user.username
Or, just user User.objects.get(id=request.user.id)
Actually what is my caffienated ass saying? You have your user object right there!
request.user : )