Is there GUID available in Django or Python?

653 views
Skip to first unread message

Chuck22

unread,
Dec 16, 2008, 11:15:46 PM12/16/08
to Django users
1. I want to create Global Unique ID (GUID) as the primary key of my
database table. Is there GUID available in Django or Python?

2. Also, I need to create a Authorization Code (random char/number
string) to verify user's email when people sign up on my site. Is
there generic method in Django or Python to create such code?

Malcolm Tredinnick

unread,
Dec 16, 2008, 11:24:04 PM12/16/08
to django...@googlegroups.com

On Tue, 2008-12-16 at 20:15 -0800, Chuck22 wrote:
> 1. I want to create Global Unique ID (GUID) as the primary key of my
> database table. Is there GUID available in Django or Python?

The uuid module is part of Python's standard library. Lots of options
there for generating various forms of uuid values.

>
> 2. Also, I need to create a Authorization Code (random char/number
> string) to verify user's email when people sign up on my site. Is
> there generic method in Django or Python to create such code?

The random module is your friend here:

''.join([random.choice('abcdefghijklmnopqrstuvwxyz0123456789') for i in range(20)])

is one possibility. For more advanced stuff, have a look at
django/contrib/auth/tokens.py, although that will require reading and
comprehending some code.

Regards,
Malcolm

panta

unread,
Dec 17, 2008, 4:03:39 AM12/17/08
to Django users
<shamelessplug>
The softwarefabrica.django.utils library includes, among other things,
an UUIDField which you can use in your Django models as the primary
key. In fact I'm using it for all my new projects.
An example of usage:

from softwarefabrica.django.utils.UUIDField import UUIDField

class Wiki(models.Model):
uuid = UUIDField(_("uuid"), primary_key=True, db_index=True)
...

You can download softwarefabrica.django.utils from the cheeseshop
(PyPI):

http://pypi.python.org/pypi/softwarefabrica.django.utils

it's also installable with easy_install:

easy_install softwarefabrica.django.utils

and finally, check out also the development home page on Launchpad:

https://launchpad.net/sf-django-utils

</shamelessplug>

Ciao,
Marco
Reply all
Reply to author
Forward
0 new messages