Login

UUIDField oriented django User

Author:
bwhittington
Posted:
May 7, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

This code monkey-patches the default User model to rather use a primary key of UUIDField (see http://www.djangosnippets.org/snippets/1496/). This code also makes the email field required. This code is wildly dangerous, not completely future proof, and probably not advisable to use. If you do wish to use it, it will be easiest to implement on a fresh db with a syncdb. If you need to migrate existing user data the onus is on you to figure out an appropriate db migration plan.

I placed this code in a models.py, but it is probably more reliably placed in urls.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.db import models
from django.db.models.fields import AutoField
from django.contrib.auth.models import User
from fields import UUIDField

if type(User._meta.pk) != type(UUIDField()):
    for f in User._meta.local_fields:
        if f.name == 'email':
            f.null = False
            f.blank = False
        if f.name == 'id':
            User._meta.local_fields.remove(f)
    User._meta.pk = None
    uuidfield = UUIDField(primary_key=True)
    User.add_to_class("id", uuidfield)
    temp = User._meta.local_fields.pop()
    User._meta.local_fields = [temp] + User._meta.local_fields

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months, 4 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.