Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Generate a unique username for django.contrib.auth [RSS Feed]

#1 Jan. 12, 2011 21:11:59

Micah C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


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.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 12, 2011 21:21:05

marco c.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


Hi Micah

On Jan 12, 10:11 pm, Micah Carrick <mi...@greentackle.com> 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.
>

couldn't you use the user.id after creating it?
i.e.

prefix = 'user'
###some code to save user###

user.your_field = prefix + str(user.id)
user.save()

if you don't neet this value in the database you could put in your
mail directly the user.id
<h1>hello {{ user.id }}</h1>

unfortunately I cannot test this solution now (sorry, i've not django
in this machine)

hoping this help,
marco

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 12, 2011 21:23:11

Eric C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


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.
> 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.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 12, 2011 21:52:35

A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


Why not just use incremental numeric user IDs?

On 12 January 2011 21:23, 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.
>> 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.
> 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.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 12, 2011 21:53:55

Micah C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


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()
data = '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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 12, 2011 22:39:51

Eric C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


Because the id is not known until after the record is saved, so you'd have to
generate some non-colliding filler value anyway. Using incremental numbers can
also leak usage information to the users.


On Jan 12, 2011, at 1:52 PM, Acorn wrote:

> Why not just use incremental numeric user IDs?
>
> On 12 January 2011 21:23, 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.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 14, 2011 21:17:17

Ian C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


On Wed, Jan 12, 2011 at 1:11 PM, Micah Carrick <mi...@greentackle.com> wrote:
> I cannot use uuid4().hex because that's 32 characters... I need <30.

uuid4().hex is almost as random as uuid4().hex

There are no timers, MAC addresses, or other non-random sections in a
UUID4, so taking any 30-character slice from it should get you 120
bits of pure random goodness.

--
Regards,
Ian Clelland
<clell...@gmail.com>

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

Offline

#8 Jan. 15, 2011 15:53:42

Martin J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Generate a unique username for django.contrib.auth


> 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.

Note that you can fix that quite easily by putting something like
this

for f in User._meta.fields:
if f.name == "username":
f.max_length=75

somewhere in your code. Easiest in your models.py.

mjl

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

Offline

  • Root
  • » Django
  • » Generate a unique username for django.contrib.auth [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support