# This is a simple gawk script that extracts the contents of your "class Admin" classes from the # given model file and prints out a ModelAdmin definition including the admin.site.register lines. # This will not do anything to your existing file. # # To use: # 1. While in the django application directory run this script as: # gawk -f admin_model.awk models.py > additional_code.py # 2. Verify that the contents of additional_code.py look good. # 3. Copy the contents of additional_code.py to the end of models.py. # 4. Use your favorite editor fix the indentation. # 5. Delete the existing "class Admin" definition from models.py file. # 6. Add 'from django.contrib import admin' to the top of models.py file. # 7. Update the code to reflect any other incompatible changes (like raw_id_admin). # # Blog entry about this script: # http://www.thebitguru.com/blog/view/266-gawk%20script%20for%20updating%20your%20models.py # # List of incompatible changes: # http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Mergednewforms-adminintotrunk # # Author: Farhan Ahmad # 2008-07-23 { if ($0 ~ /class /) { admin_class = 0; if ($0 ~ /\(models.Model\):/) { match($0, /^class (.*)\(models.Model\):.*/, arr) last_model = arr[1]; } else if ($0 ~ /class Admin:/) { classes[NR] = last_model printf "class %sAdmin(admin.ModelAdmin):\n", last_model admin_class = 1 } } if ($0 !~ /class Admin/ && admin_class == 1) { print $0 } } END { printf "\n" for (idx in classes) { class = classes[idx] printf "admin.site.register(%s, %sAdmin)\n", class, class } }