thanx,
-- Ben
from django.contrib.auth.models import User
class BasicBackend:
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
class EmailBackend(BasicBackend):
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
and add to settings.py
AUTHENTICATION_BACKENDS = (
'publicads.auth.EmailBackend',
)
Also I autogenerate usernames based on id, something like 'user_5454'.
Works for me
I tried something like this:
from django.contrib.auth.models import User as OldUser
#from django.db import models
class User(OldUser):
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
#OldUser.email.blank=False
#OldUser.email.unique=True
class Admin:
pass
But the type of User.email is transferred to 'str' instead of
'CharField' so I'm stuck here.
Thanks.
-- John
You cannot override an existing class. In this case, you are going to
have to change the existing Django model. Sorry about that.
Regards,
Malcolm
You can add unique constraint on email column on database level,
Django will be unaware of it, but at least database will guarantee
that emails will be unique.
--
Vasily Sulatskov
from django.core import validators
from django.contrib.auth.models import User as OldUser
from django.db import models
class User(OldUser):
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
username = models.CharField(
_('username'), blank=True, maxlength=30, unique=False, \
validator_list=[validators.isAlphaNumeric], \
help_text=_("Required. 30 characters or fewer. Alphanumeric "\
"characters only (letters, digits and
underscores)."))
email = models.EmailField(_('e-mail address'), blank=False,
unique=True)
# additional fields, etc.
class Admin:
pass
class Event(models.Model):
def __str__(self):
return self.name
name = models.CharField(maxlength=100, blank=False)
attendants = models.ManyToManyField(User)
description = models.TextField()
class Admin:
pass
This will create a new mysite_user table and mysite_event_attendants
will refer to the correct foreign tables. However, under the /admin
site, the 'event' object still refer to 'auth_user' instead of
'mysite_user', and this will cause an error whenever you add/change/
delete.
Does that means the best I can do is to use 'auth' and 'admin' apps as
a simple admin backend, and build my own models/admin apps?
-- John
On Mar 27, 8:31 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote: