Using .update() to add additional fields to django-registration?
I added the additional fields first_name and last_name to django-registration, and got it working the following way --
# `registration/backends/default/__init__.py`
from django.contrib.auth.models import User
class DefaultBackend(object):
def register(self, request, **kwargs):
username, email, first_name, last_name, password =
kwargs['email'], kwargs['email'], kwargs['first_name'], kwargs['last_name'], kwargs['password1']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = Req开发者_StackOverflow社区uestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
# to create the additional first_name, last_name fields
additional = User.objects.filter(username=username).update(first_name=first_name, last_name=last_name)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
Is using .update()
a good way to add these additional fields? If not, what would be a better method to use (for some reason, the registration process does seem to perform quite slow)? Thank you.
I guess there is no problem if you use .update()
to make the changes, although I would have personally used signals to do the needful. The benefit of using signals would be that you can clearly keep the registration logic separate from your app related logic. For eg: if you had different models for Free User and Premium User etc. or something like that.
The registration process is NOT slow. The "slowness" is most probably because the SMTP server takes time to send the mail. You can try django-mailer, which queues up the emails that need to be sent. Since it is an asynchronous process, you will need to have cron to send emails every minute or so. But it will make your registration process "faster" as the emails will not be immediately sent.
精彩评论