Thanks folks.

Here's what I have now. Not the most elegant but it's the best I can come up
with so far. First, I'm generating a random string for the username so that
the form will validate. After the record is saved I change the username to
"user_" and the user ID. This way it looks a little less scary if it pops up
on an admin form somewhere or something.

# in the view for my sign up form
if request.method == 'POST':
        data = request.POST.copy()
        randstr = ''.join([choice(string.letters) for i in xrange(30)])
        data['username'] = 'user_' + randstr
        form = SignUpForm(data) # subclass of UserCreationForm
        if form.is_valid():
            form.save()

# in the save method of SignUpForm which is subclassed from UserCreationForm
user.save()
user.username = 'user_' + str(user.id)
user.save()

What I still hate about this one is that I'm hitting the database twice for
a single registration. But, it seems to work.

On Wed, Jan 12, 2011 at 1:23 PM, Eric Chamberlain <e...@rf.com> wrote:

> We use a base64 or base36 (if you want compatibility with the
> contrib.admin) encoded UUID, or generate a random 30-character string, the
> odds of a collision is quite low.
>
> On Jan 12, 2011, at 1:11 PM, Micah Carrick wrote:
>
> > I've got my site's authentication working with and email and password
> only--no username (thanks to Shawn Milochik for helping me with that).
> However, I still need to put in a username to make the User model happy. I
> was hoping to have "user" as a prefix and then some unique number.
> >
> > I cannot simply copy the email to the username because the username must
> be less than 30 characters and, after looking into my database, many email
> addresses go over that.
> > I cannot generate a random number because there could be a collision.
> > I cannot use uuid4().hex because that's 32 characters... I need <30.
> > I cannot use User.objects.count() because that could result in a
> collision if 2 users register at the same time.
> >
> > Thoughts?
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> > For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>


-- 
Micah Carrick, Founder

    *Green Tackle* - *Environmentally Friendly Fishing Tackle*
    www.GreenTackle.com <http://www.greentackle.com>

     Email: mi...@greentackle.com
     Phone: 971.270.2206
     Toll Free: 877.580.9165
     Fax: 503.946.3106

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to