| Version 15 (modified by , 18 years ago) ( diff ) |
|---|
Removing the magic: the cheat sheet
This document assumes a basic familiarity with Removing the magic changes. It is designed to be used as a simple cheat sheet during a conversion process providing links to relevant parts of Removing the magic document. It is organized by functional areas: databases, models, views, templates, template tags, and settings. This document doesn't cover new functionality.
Pre-conversion
Optional (saves time): if your project doesn't have manage.py file in the project's directory, copy it from django/conf/project_template/.
Databases
Database changes are covered extensively in Database changes you'll need to make. Just execute appropriate SQL scripts.
Optional (saves space): package column of auth_permission table is not used anymore. You can drop it by executing following SQL statement (works for MySQL):
ALTER TABLE `auth_permission` DROP COLUMN `package`;
Eugene's note: after conversion I found that my permissions are screwed up. I regenerated them this way:
- Delete a content of following tables (you can do it now):
auth_group_permissionsauth_permissionsauth_user_user_permissionsdjango_content_type
- After you finished with code changes, run
manage.py syncdb. Don't do it now'''
It repopulates the content of said tables properly, but an administrator has to reassign group and user permissions again in Admin. Obviously it can be a problem, if you have hundreds of users with arcane permissions.
Models
- If your application still lives in
yourproject/apps/directory, you can move it up one level: moveyourproject/apps/yourapp/toyourproject/yourapp. - Relocate models by moving content of files in your
yourapp/models/directory toyourapp/models.pyfile. - If your models use
datetimeordbmodules without exporting them directly, add import statements. - Relocate Model and Field classes: use
django.db.modelsinstead ofdjango.core.meta. - Convert the META.admin member to new inner
class Adminof the model. Don't forget to remove all pesky commas at the end of lines. - Remove from META following parameters:
admin(see the previous step),module_name,exceptions,module_constants, andwhere_constraints. If now yourclass METAis empty, delete it. Otherwise rename it toclass Meta. - Replace __repr__ with __str__.
- If you use
_pre_save(),_post_save(),_pre_delete(), and/or_post_delete()hooks, replace them by overridingsave()anddelete()methods. - If your model has
objectsattribute, rename it.
Views
- Import your models directly the Python way. For example, if your model
Personis inyourproject/yourapp/models.pyfile, use:from yourproject.yourapp.models import Person
- Change import statements to reflect the namespace simplification:
django.utils.httpwrappers=>django.httpdjango.core.exceptions.Http404=>django.http.Http404django.core.template=>django.templatedjango.core.formfields=>django.formsdjango.core.extensions=>django.shortcuts- renamed:
django.core.extensions.DjangoContext=>django.template.RequestContext - moved:
django.models.core.Session=>django.contrib.sessions.models.Session
- A former module
settingsis an instance now. Import it using:from django.conf import settings
- Update your code to reflect model changes you did in the Models section above.
- Include template extensions explicitly.
- Convert to new Database API. Warning: usually this is the most time-consuming step. Be careful!
- Rename DoesNotExist exception:
people.PersonDoesNotExistbecomesPerson.DoesNotExist. - If you use
get_object_or_404()andget_list_or_404(), change their parameters. Note: you cannot use field__neandorder_bykeywords. Useexclude()andorder_by()methods of new Database API. - If you call generic views from your views:
- Update their parameters: remove
app_labelandmodel_name, addqueryset. - If you pass a
template_nameargument, add the explicit template extension.
- Update their parameters: remove
- If you use Django's authentication, update your code to reflect the consolidation.
- If you use manipulators, update your code:
django.core.formfieldsisdjango.formsnow. - If you use the syndication framework, specify .html extension for slug templates explicitly.
- If you use raw database connections, update your code.
- The
packagesmodule has been removed.
Templates
- Optional (file consolidation): you can relocate your templates from
yourproject/templates/yourapp/directory toyourproject/yourapp/templates/directory. - Template tags
extendsandincludeshould include template extensions explicitly. - Change template names for generic views: remove pluralization of model names.
- If your template uses Django's methods defined on its parameters, try to move this code to corresponding views, if possible. Otherwise apply changes outlined in the Views section above.
Template tags
- Change import statements to reflect the namespace simplification:
django.core.template=>django.templatedjango.core.extensions=>django.shortcuts- renamed:
django.core.extensions.DjangoContext=>django.template.RequestContext
- If your template tag uses external templates, include template extensions explicitly.
- SilentVariableFailure exception was removed: use
silent_variable_failureattribute instead. - Update your code to reflect model changes you did in the Models section above, if applicable.
- If you create a template tag via
inclusion_tag(), specify its extension explicitly. - If your template tag implements a high-level view-like functionality, apply changes outlined in the Views section above.
URLs and settings
- Update
urls.py:- Admin URLConf was relocated:
django.contrib.admin.urls.admin=>django.contrib.admin.urls
- Change the parameters you pass to generic views: remove
app_labelandmodel_name, addqueryset. - If you pass a
template_nameargument to generic views, add the explicit template extension.
- Admin URLConf was relocated:
- Update
settings.py:- Update
TEMPLATE_LOADERSto reflect Namespace simplification: removecorefrom their names. - If you moved templates to
yourappdirectory (see the Templates section), make sure yourTEMPLATE_LOADERSvariable includesdjango.template.loaders.app_directories.load_template_source. - Update
INSTALLED_APPSto reflect Namespace simplification:django.models.auth=>django.contrib.auth.modelsdjango.models.core.sites=>django.contrib.sites.modelsdjango.models.core.contenttypes=>django.contrib.contenttypes.models
- Update
INSTALLED_APPSto reflect changes you made in the Models section:yourproject.apps.yourapp=>yourproject.yourapp
- Add additional applications to your
INSTALLED_APPSto satisfy dependencies:- Before
django.contrib.adminadddjango.contrib.authanddjango.contrib.contenttypes. - Before
django.contrib.flatpagesand/ordjango.contrib.redirectsadddjango.contrib.sites.
- Before
- The
packagesmodule has been removed. - Update
MIDDLEWARE_CLASSESto reflect Namespace simplification:django.middleware.sessions.SessionMiddleware=>django.contrib.sessions.middleware.SessionMiddleware
- If you rely on
request.useranywhere in your code, modifyMIDDLEWARE_CLASSES:- Add
django.contrib.auth.middleware.AuthenticationMiddlewareanywhere afterdjango.contrib.sessions.middleware.SessionMiddleware.
- Add
- Remove
TEMPLATE_FILE_EXTENSION--- it's not used anymore.
- Update
Post-conversion
Debug your changes using manage.py validate and manage.py runserver. If there are any problems, please refer to the appropriate section of this document to verify your changes, and consult Removing the magic.
If you decided to regenerate permissions (see the Pre-conversion section above), now is the good time to execute manage.py syncdb. Remember that an administrator should reassign permissions to all users using Admin.