Login

Alter Column Lengths of Contrib Apps

Author:
guettli
Posted:
April 23, 2008
Language:
Python
Version:
.96
Tags:
signals
Score:
1 (after 1 ratings)

If you want to modify the length of a column of a contrib application, you can either modify django (cumbersome) or you can run a post_syncdb signal hook.

Related: Ticket 4748

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# This code needs to be in management.py
from django.contrib.auth import models as auth_models
# Related ticket http://code.djangoproject.com/ticket/4748
def alter_django_auth_permissions(sender, **kwargs):
    if not auth_models.Permission in kwargs['created_models']:
        return
    SIZE_NAME=128
    cursor=connection.cursor()
    cursor.execute("SELECT * FROM auth_permission LIMIT 1")
    
    for desc in cursor.description:
        # See http://www.python.org/dev/peps/pep-0249/
        name, type_code, display_size, internal_size, precision, scale, null_ok = desc
        if not name=='name':
            continue
        if internal_size<SIZE_NAME:
            logging.info('auth_permission: Column "name" gets altered. Old: %d new: %d' % (
                    internal_size, SIZE_NAME))
            cursor.execute('''ALTER TABLE auth_permission ALTER COLUMN "name" type VARCHAR(%s)''',
                           [SIZE_NAME])
        break
    else:
        raise Exception('table auth_permission has not column "name"')
django.db.models.signals.post_syncdb.connect(alter_django_auth_permissions)
    

More like this

  1. More admin clock time increments by robharvey 8 years, 2 months ago
  2. CheckboxSelectMultiple that renders in columns by rfugger 4 years, 8 months ago
  3. easy signal registration by aisbaa 3 years, 9 months ago
  4. Generic Recievers For Signals by ericmoritz 7 years, 1 month ago
  5. Twin column model admin index by richardbolt 7 years, 3 months ago

Comments

Please login first before commenting.