2008-09-02 16:27:47 +00:00
|
|
|
=========================================
|
|
|
|
Porting your apps from Django 0.96 to 1.0
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
.. highlight:: python
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Django 1.0 breaks compatibility with 0.96 in some areas.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
This guide will help you port 0.96 projects and apps to 1.0. The first part of
|
|
|
|
this document includes the common changes needed to run with 1.0. If after going
|
|
|
|
through the first part your code still breaks, check the section `Less-common
|
|
|
|
Changes`_ for a list of a bunch of less-common compatibility issues.
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
The :doc:`1.0 release notes </releases/1.0>`. That document explains the new
|
2008-09-02 16:27:47 +00:00
|
|
|
features in 1.0 more deeply; the porting guide is more concerned with
|
|
|
|
helping you quickly update your code.
|
|
|
|
|
|
|
|
Common changes
|
|
|
|
==============
|
|
|
|
|
|
|
|
This section describes the changes between 0.96 and 1.0 that most users will
|
|
|
|
need to make.
|
|
|
|
|
|
|
|
Use Unicode
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Change string literals (``'foo'``) into Unicode literals (``u'foo'``). Django
|
|
|
|
now uses Unicode strings throughout. In most places, raw strings will continue
|
|
|
|
to work, but updating to use Unicode literals will prevent some obscure
|
|
|
|
problems.
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
See :doc:`/ref/unicode` for full details.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Models
|
|
|
|
------
|
|
|
|
|
|
|
|
Common changes to your models file:
|
|
|
|
|
|
|
|
Rename ``maxlength`` to ``max_length``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Rename your ``maxlength`` argument to ``max_length`` (this was changed to be
|
2008-09-07 19:15:41 +00:00
|
|
|
consistent with form fields):
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Replace ``__str__`` with ``__unicode__``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Replace your model's ``__str__`` function with a ``__unicode__`` method, and
|
|
|
|
make sure you `use Unicode`_ (``u'foo'``) in that method.
|
|
|
|
|
|
|
|
Remove ``prepopulated_from``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Remove the ``prepopulated_from`` argument on model fields. It's no longer valid
|
2008-10-09 23:35:34 +00:00
|
|
|
and has been moved to the ``ModelAdmin`` class in ``admin.py``. See `the
|
2008-09-02 16:27:47 +00:00
|
|
|
admin`_, below, for more details about changes to the admin.
|
|
|
|
|
2008-10-06 08:30:02 +00:00
|
|
|
Remove ``core``
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Remove the ``core`` argument from your model fields. It is no longer
|
|
|
|
necessary, since the equivalent functionality (part of :ref:`inline editing
|
|
|
|
<admin-inlines>`) is handled differently by the admin interface now. You don't
|
|
|
|
have to worry about inline editing until you get to `the admin`_ section,
|
|
|
|
below. For now, remove all references to ``core``.
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
Replace ``class Admin:`` with ``admin.py``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Remove all your inner ``class Admin`` declarations from your models. They won't
|
|
|
|
break anything if you leave them, but they also won't do anything. To register
|
|
|
|
apps with the admin you'll move those declarations to an ``admin.py`` file;
|
|
|
|
see `the admin`_ below for more details.
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
A contributor to djangosnippets__ has written a script that'll `scan your
|
|
|
|
models.py and generate a corresponding admin.py`__.
|
|
|
|
|
|
|
|
__ http://www.djangosnippets.org/
|
|
|
|
__ http://www.djangosnippets.org/snippets/603/
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
Example
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Below is an example ``models.py`` file with all the changes you'll need to make:
|
|
|
|
|
|
|
|
Old (0.96) ``models.py``::
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
first_name = models.CharField(maxlength=30)
|
|
|
|
last_name = models.CharField(maxlength=30)
|
|
|
|
slug = models.CharField(maxlength=60, prepopulate_from=('first_name', 'last_name'))
|
|
|
|
|
|
|
|
class Admin:
|
|
|
|
list_display = ['first_name', 'last_name']
|
|
|
|
|
2008-09-02 18:38:55 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '%s %s' % (self.first_name, self.last_name)
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
New (1.0) ``models.py``::
|
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
slug = models.CharField(max_length=60)
|
|
|
|
|
2008-09-02 18:38:55 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s %s' % (self.first_name, self.last_name)
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
New (1.0) ``admin.py``::
|
|
|
|
|
|
|
|
from django.contrib import admin
|
|
|
|
from models import Author
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class AuthorAdmin(admin.ModelAdmin):
|
2008-09-02 18:38:55 +00:00
|
|
|
list_display = ['first_name', 'last_name']
|
2008-09-02 16:27:47 +00:00
|
|
|
prepopulated_fields = {
|
|
|
|
'slug': ('first_name', 'last_name')
|
|
|
|
}
|
|
|
|
|
|
|
|
admin.site.register(Author, AuthorAdmin)
|
|
|
|
|
|
|
|
The Admin
|
|
|
|
---------
|
|
|
|
|
|
|
|
One of the biggest changes in 1.0 is the new admin. The Django administrative
|
|
|
|
interface (``django.contrib.admin``) has been completely refactored; admin
|
|
|
|
definitions are now completely decoupled from model definitions, the framework
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
has been rewritten to use Django's new form-handling library and redesigned with
|
2008-09-02 16:27:47 +00:00
|
|
|
extensibility and customization in mind.
|
|
|
|
|
2008-09-02 18:38:55 +00:00
|
|
|
Practically, this means you'll need to rewrite all of your ``class Admin``
|
2008-09-02 16:27:47 +00:00
|
|
|
declarations. You've already seen in `models`_ above how to replace your ``class
|
|
|
|
Admin`` with a ``admin.site.register()`` call in an ``admin.py`` file. Below are
|
|
|
|
some more details on how to rewrite that ``Admin`` declaration into the new
|
|
|
|
syntax.
|
|
|
|
|
|
|
|
Use new inline syntax
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The new ``edit_inline`` options have all been moved to ``admin.py``. Here's an
|
|
|
|
example:
|
|
|
|
|
|
|
|
Old (0.96)::
|
|
|
|
|
|
|
|
class Parent(models.Model):
|
|
|
|
...
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class Child(models.Model):
|
|
|
|
parent = models.ForeignKey(Parent, edit_inline=models.STACKED, num_in_admin=3)
|
|
|
|
|
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
class ChildInline(admin.StackedInline):
|
|
|
|
model = Child
|
|
|
|
extra = 3
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-12 16:56:09 +00:00
|
|
|
class ParentAdmin(admin.ModelAdmin):
|
2008-09-02 16:27:47 +00:00
|
|
|
model = Parent
|
|
|
|
inlines = [ChildInline]
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
admin.site.register(Parent, ParentAdmin)
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
See :ref:`admin-inlines` for more details.
|
|
|
|
|
2008-09-02 19:04:33 +00:00
|
|
|
Simplify ``fields``, or use ``fieldsets``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
The old ``fields`` syntax was quite confusing, and has been simplified. The old
|
|
|
|
syntax still works, but you'll need to use ``fieldsets`` instead.
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Old (0.96)::
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
class ModelOne(models.Model):
|
|
|
|
...
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class Admin:
|
|
|
|
fields = (
|
|
|
|
(None, {'fields': ('foo','bar')}),
|
|
|
|
)
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class ModelTwo(models.Model):
|
|
|
|
...
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class Admin:
|
|
|
|
fields = (
|
|
|
|
('group1', {'fields': ('foo','bar'), 'classes': 'collapse'}),
|
|
|
|
('group2', {'fields': ('spam','eggs'), 'classes': 'collapse wide'}),
|
|
|
|
)
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
class ModelOneAdmin(admin.ModelAdmin):
|
|
|
|
fields = ('foo', 'bar')
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
class ModelTwoAdmin(admin.ModelAdmin):
|
|
|
|
fieldsets = (
|
|
|
|
('group1', {'fields': ('foo','bar'), 'classes': 'collapse'}),
|
|
|
|
('group2', {'fields': ('spam','eggs'), 'classes': 'collapse wide'}),
|
|
|
|
)
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
* More detailed information about the changes and the reasons behind them
|
|
|
|
can be found on the `NewformsAdminBranch wiki page`__
|
|
|
|
|
|
|
|
* The new admin comes with a ton of new features; you can read about them in
|
2010-08-19 19:27:44 +00:00
|
|
|
the :doc:`admin documentation </ref/contrib/admin/index>`.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2012-03-13 17:53:31 +00:00
|
|
|
__ https://code.djangoproject.com/wiki/NewformsAdminBranch
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-02 19:08:31 +00:00
|
|
|
URLs
|
2008-09-02 16:27:47 +00:00
|
|
|
----
|
|
|
|
|
|
|
|
Update your root ``urls.py``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
If you're using the admin site, you need to update your root ``urls.py``.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Old (0.96) ``urls.py``::
|
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
(r'^admin/', include('django.contrib.admin.urls')),
|
|
|
|
|
|
|
|
# ... the rest of your URLs here ...
|
|
|
|
)
|
|
|
|
|
|
|
|
New (1.0) ``urls.py``::
|
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
|
|
|
|
|
|
|
# The next two lines enable the admin and load each admin.py file:
|
|
|
|
from django.contrib import admin
|
|
|
|
admin.autodiscover()
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
(r'^admin/(.*)', admin.site.root),
|
|
|
|
|
|
|
|
# ... the rest of your URLs here ...
|
|
|
|
)
|
|
|
|
|
|
|
|
Views
|
|
|
|
-----
|
|
|
|
|
|
|
|
Use ``django.forms`` instead of ``newforms``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Replace ``django.newforms`` with ``django.forms`` -- Django 1.0 renamed the
|
|
|
|
``newforms`` module (introduced in 0.96) to plain old ``forms``. The
|
2008-09-05 00:48:34 +00:00
|
|
|
``oldforms`` module was also removed.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
If you're already using the ``newforms`` library, and you used our recommended
|
|
|
|
``import`` statement syntax, all you have to do is change your import
|
|
|
|
statements.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Old::
|
|
|
|
|
|
|
|
from django import newforms as forms
|
|
|
|
|
|
|
|
New::
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
|
|
|
If you're using the old forms system (formerly known as ``django.forms`` and
|
|
|
|
``django.oldforms``), you'll have to rewrite your forms. A good place to start
|
2010-08-19 19:27:44 +00:00
|
|
|
is the :doc:`forms documentation </topics/forms/index>`
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Handle uploaded files using the new API
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Replace use of uploaded files -- that is, entries in ``request.FILES`` -- as
|
|
|
|
simple dictionaries with the new :class:`~django.core.files.UploadedFile`. The
|
|
|
|
old dictionary syntax no longer works.
|
|
|
|
|
|
|
|
Thus, in a view like::
|
|
|
|
|
|
|
|
def my_view(request):
|
|
|
|
f = request.FILES['file_field_name']
|
|
|
|
...
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
...you'd need to make the following changes:
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
===================== =====================
|
|
|
|
Old (0.96) New (1.0)
|
|
|
|
===================== =====================
|
|
|
|
``f['content']`` ``f.read()``
|
|
|
|
``f['filename']`` ``f.name``
|
|
|
|
``f['content-type']`` ``f.content_type``
|
|
|
|
===================== =====================
|
|
|
|
|
2008-10-06 08:30:02 +00:00
|
|
|
Work with file fields using the new API
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The internal implementation of :class:`django.db.models.FileField` have changed.
|
|
|
|
A visible result of this is that the way you access special attributes (URL,
|
|
|
|
filename, image size, etc) of these model fields has changed. You will need to
|
|
|
|
make the following changes, assuming your model's
|
|
|
|
:class:`~django.db.models.FileField` is called ``myfile``:
|
|
|
|
|
|
|
|
=================================== ========================
|
|
|
|
Old (0.96) New (1.0)
|
|
|
|
=================================== ========================
|
|
|
|
``myfile.get_content_filename()`` ``myfile.content.path``
|
|
|
|
``myfile.get_content_url()`` ``myfile.content.url``
|
|
|
|
``myfile.get_content_size()`` ``myfile.content.size``
|
|
|
|
``myfile.save_content_file()`` ``myfile.content.save()``
|
|
|
|
``myfile.get_content_width()`` ``myfile.content.width``
|
|
|
|
``myfile.get_content_height()`` ``myfile.content.height``
|
|
|
|
=================================== ========================
|
|
|
|
|
|
|
|
Note that the ``width`` and ``height`` attributes only make sense for
|
|
|
|
:class:`~django.db.models.ImageField` fields. More details can be found in the
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`model API </ref/models/fields>` documentation.
|
2008-10-06 08:30:02 +00:00
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 18:30:54 +00:00
|
|
|
Use ``Paginator`` instead of ``ObjectPaginator``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The ``ObjectPaginator`` in 0.96 has been removed and replaced with an improved
|
|
|
|
version, :class:`django.core.paginator.Paginator`.
|
2008-10-06 08:30:02 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
Templates
|
|
|
|
---------
|
|
|
|
|
|
|
|
Learn to love autoescaping
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
By default, the template system now automatically HTML-escapes the output of
|
2008-09-02 16:27:47 +00:00
|
|
|
every variable. To learn more, see :ref:`automatic-html-escaping`.
|
|
|
|
|
|
|
|
To disable auto-escaping for an individual variable, use the :tfilter:`safe`
|
|
|
|
filter:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
This will be escaped: {{ data }}
|
|
|
|
This will not be escaped: {{ data|safe }}
|
|
|
|
|
|
|
|
To disable auto-escaping for an entire template, wrap the template (or just a
|
|
|
|
particular section of the template) in the :ttag:`autoescape` tag:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
... unescaped template content here ...
|
|
|
|
{% endautoescape %}
|
|
|
|
|
|
|
|
Less-common changes
|
|
|
|
===================
|
|
|
|
|
|
|
|
The following changes are smaller, more localized changes. They should only
|
|
|
|
affect more advanced users, but it's probably worth reading through the list and
|
|
|
|
checking your code for these things.
|
|
|
|
|
|
|
|
Signals
|
|
|
|
-------
|
|
|
|
|
|
|
|
* Add ``**kwargs`` to any registered signal handlers.
|
|
|
|
|
|
|
|
* Connect, disconnect, and send signals via methods on the
|
|
|
|
:class:`~django.dispatch.Signal` object instead of through module methods in
|
|
|
|
``django.dispatch.dispatcher``.
|
|
|
|
|
|
|
|
* Remove any use of the ``Anonymous`` and ``Any`` sender options; they no longer
|
|
|
|
exist. You can still receive signals sent by any sender by using
|
|
|
|
``sender=None``
|
|
|
|
|
|
|
|
* Make any custom signals you've declared into instances of
|
2010-11-28 17:49:43 +00:00
|
|
|
:class:`django.dispatch.Signal` instead of anonymous objects.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Here's quick summary of the code changes you'll need to make:
|
|
|
|
|
|
|
|
================================================= ======================================
|
|
|
|
Old (0.96) New (1.0)
|
|
|
|
================================================= ======================================
|
|
|
|
``def callback(sender)`` ``def callback(sender, **kwargs)``
|
|
|
|
``sig = object()`` ``sig = django.dispatch.Signal()``
|
|
|
|
``dispatcher.connect(callback, sig)`` ``sig.connect(callback)``
|
|
|
|
``dispatcher.send(sig, sender)`` ``sig.send(sender)``
|
|
|
|
``dispatcher.connect(callback, sig, sender=Any)`` ``sig.connect(callback, sender=None)``
|
|
|
|
================================================= ======================================
|
|
|
|
|
2008-09-03 19:58:34 +00:00
|
|
|
Comments
|
|
|
|
--------
|
|
|
|
|
|
|
|
If you were using Django 0.96's ``django.contrib.comments`` app, you'll need to
|
2012-11-24 17:10:51 +00:00
|
|
|
upgrade to the new comments app introduced in 1.0. See the upgrade guide
|
|
|
|
for details.
|
2008-09-03 19:58:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
Template tags
|
|
|
|
-------------
|
|
|
|
|
|
|
|
:ttag:`spaceless` tag
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
The spaceless template tag now removes *all* spaces between HTML tags, instead
|
|
|
|
of preserving a single space.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Local flavors
|
|
|
|
-------------
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
U.S. local flavor
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
``django.contrib.localflavor.usa`` has been renamed to
|
|
|
|
:mod:`django.contrib.localflavor.us`. This change was made to match the naming
|
|
|
|
scheme of other local flavors. To migrate your code, all you need to do is
|
|
|
|
change the imports.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Sessions
|
|
|
|
--------
|
|
|
|
|
|
|
|
Getting a new session key
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2009-01-29 20:21:43 +00:00
|
|
|
``SessionBase.get_new_session_key()`` has been renamed to
|
2008-09-05 00:48:34 +00:00
|
|
|
``_get_new_session_key()``. ``get_new_session_object()`` no longer exists.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Fixtures
|
|
|
|
--------
|
|
|
|
|
|
|
|
Loading a row no longer calls ``save()``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Previously, loading a row automatically ran the model's ``save()`` method. This
|
|
|
|
is no longer the case, so any fields (for example: timestamps) that were
|
|
|
|
auto-populated by a ``save()`` now need explicit values in any fixture.
|
|
|
|
|
|
|
|
Settings
|
|
|
|
--------
|
|
|
|
|
|
|
|
Better exceptions
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
The old :exc:`EnvironmentError` has split into an :exc:`ImportError` when
|
2008-09-02 16:27:47 +00:00
|
|
|
Django fails to find the settings module and a :exc:`RuntimeError` when you try
|
|
|
|
to reconfigure settings after having already used them
|
|
|
|
|
2011-05-29 17:41:04 +00:00
|
|
|
:setting:`LOGIN_URL` has moved
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2011-05-29 17:41:04 +00:00
|
|
|
The :setting:`LOGIN_URL` constant moved from ``django.contrib.auth`` into the
|
2008-09-02 16:27:47 +00:00
|
|
|
``settings`` module. Instead of using ``from django.contrib.auth import
|
|
|
|
LOGIN_URL`` refer to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
:setting:`APPEND_SLASH` behavior has been updated
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
In 0.96, if a URL didn't end in a slash or have a period in the final
|
2011-05-29 17:41:04 +00:00
|
|
|
component of its path, and :setting:`APPEND_SLASH` was True, Django would
|
|
|
|
redirect to the same URL, but with a slash appended to the end. Now, Django
|
|
|
|
checks to see whether the pattern without the trailing slash would be matched
|
|
|
|
by something in your URL patterns. If so, no redirection takes place, because
|
|
|
|
it is assumed you deliberately wanted to catch that pattern.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
For most people, this won't require any changes. Some people, though, have URL
|
|
|
|
patterns that look like this::
|
|
|
|
|
|
|
|
r'/some_prefix/(.*)$'
|
|
|
|
|
|
|
|
Previously, those patterns would have been redirected to have a trailing
|
|
|
|
slash. If you always want a slash on such URLs, rewrite the pattern as::
|
|
|
|
|
|
|
|
r'/some_prefix/(.*/)$'
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Smaller model changes
|
2008-09-02 16:27:47 +00:00
|
|
|
---------------------
|
|
|
|
|
|
|
|
Different exception from ``get()``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Managers now return a :exc:`MultipleObjectsReturned` exception
|
2008-09-02 16:27:47 +00:00
|
|
|
instead of :exc:`AssertionError`:
|
|
|
|
|
|
|
|
Old (0.96)::
|
|
|
|
|
|
|
|
try:
|
|
|
|
Model.objects.get(...)
|
|
|
|
except AssertionError:
|
|
|
|
handle_the_error()
|
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
try:
|
|
|
|
Model.objects.get(...)
|
|
|
|
except Model.MultipleObjectsReturned:
|
|
|
|
handle_the_error()
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
``LazyDate`` has been fired
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The ``LazyDate`` helper class no longer exists.
|
|
|
|
|
|
|
|
Default field values and query arguments can both be callable objects, so
|
|
|
|
instances of ``LazyDate`` can be replaced with a reference to ``datetime.datetime.now``:
|
|
|
|
|
|
|
|
Old (0.96)::
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(maxlength=100)
|
|
|
|
published = models.DateField(default=LazyDate())
|
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
class Article(models.Model):
|
2008-09-07 19:15:41 +00:00
|
|
|
title = models.CharField(max_length=100)
|
2008-09-02 16:27:47 +00:00
|
|
|
published = models.DateField(default=datetime.datetime.now)
|
|
|
|
|
|
|
|
``DecimalField`` is new, and ``FloatField`` is now a proper float
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Old (0.96)::
|
|
|
|
|
|
|
|
class MyModel(models.Model):
|
|
|
|
field_name = models.FloatField(max_digits=10, decimal_places=3)
|
|
|
|
...
|
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
class MyModel(models.Model):
|
|
|
|
field_name = models.DecimalField(max_digits=10, decimal_places=3)
|
|
|
|
...
|
|
|
|
|
|
|
|
If you forget to make this change, you will see errors about ``FloatField``
|
2008-09-05 00:48:34 +00:00
|
|
|
not taking a ``max_digits`` attribute in ``__init__``, because the new
|
2008-09-02 16:27:47 +00:00
|
|
|
``FloatField`` takes no precision-related arguments.
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
If you're using MySQL or PostgreSQL, no further changes are needed. The
|
2008-09-02 16:27:47 +00:00
|
|
|
database column types for ``DecimalField`` are the same as for the old
|
|
|
|
``FloatField``.
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
If you're using SQLite, you need to force the database to view the
|
2008-09-02 16:27:47 +00:00
|
|
|
appropriate columns as decimal types, rather than floats. To do this, you'll
|
|
|
|
need to reload your data. Do this after you have made the change to using
|
|
|
|
``DecimalField`` in your code and updated the Django code.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
**Back up your database first!**
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
For SQLite, this means making a copy of the single file that stores the
|
2011-05-29 17:41:04 +00:00
|
|
|
database (the name of that file is the :setting:`DATABASE_NAME` in your
|
|
|
|
settings.py file).
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
To upgrade each application to use a ``DecimalField``, you can do the
|
|
|
|
following, replacing ``<app>`` in the code below with each app's name:
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ ./manage.py dumpdata --format=xml <app> > data-dump.xml
|
|
|
|
$ ./manage.py reset <app>
|
|
|
|
$ ./manage.py loaddata data-dump.xml
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
1. It's important that you remember to use XML format in the first step of
|
|
|
|
this process. We are exploiting a feature of the XML data dumps that makes
|
|
|
|
porting floats to decimals with SQLite possible.
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
2. In the second step you will be asked to confirm that you are prepared to
|
|
|
|
lose the data for the application(s) in question. Say yes; we'll restore
|
|
|
|
this data in the third step, of course.
|
2008-09-05 00:48:34 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
3. ``DecimalField`` is not used in any of the apps shipped with Django prior
|
|
|
|
to this change being made, so you do not need to worry about performing
|
|
|
|
this procedure for any of the standard Django models.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
If something goes wrong in the above process, just copy your backed up
|
2008-09-07 19:15:41 +00:00
|
|
|
database file over the original file and start again.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Internationalization
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
:func:`django.views.i18n.set_language` now requires a POST request
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Previously, a GET request was used. The old behavior meant that state (the
|
|
|
|
locale used to display the site) could be changed by a GET request, which is
|
|
|
|
against the HTTP specification's recommendations. Code calling this view must
|
|
|
|
ensure that a POST request is now made, instead of a GET. This means you can
|
|
|
|
no longer use a link to access the view, but must use a form submission of
|
|
|
|
some kind (e.g. a button).
|
|
|
|
|
|
|
|
``_()`` is no longer in builtins
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
``_()`` (the callable object whose name is a single underscore) is no longer
|
|
|
|
monkeypatched into builtins -- that is, it's no longer available magically in
|
|
|
|
every module.
|
|
|
|
|
|
|
|
If you were previously relying on ``_()`` always being present, you should now
|
|
|
|
explicitly import ``ugettext`` or ``ugettext_lazy``, if appropriate, and alias
|
|
|
|
it to ``_`` yourself::
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
HTTP request/response objects
|
|
|
|
-----------------------------
|
|
|
|
|
2008-10-05 16:03:58 +00:00
|
|
|
Dictionary access to ``HttpRequest``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
``HttpRequest`` objects no longer directly support dictionary-style
|
|
|
|
access; previously, both ``GET`` and ``POST`` data were directly
|
|
|
|
available on the ``HttpRequest`` object (e.g., you could check for a
|
|
|
|
piece of form data by using ``if 'some_form_key' in request`` or by
|
|
|
|
reading ``request['some_form_key']``. This is no longer supported; if
|
|
|
|
you need access to the combined ``GET`` and ``POST`` data, use
|
|
|
|
``request.REQUEST`` instead.
|
|
|
|
|
|
|
|
It is strongly suggested, however, that you always explicitly look in
|
|
|
|
the appropriate dictionary for the type of request you expect to
|
|
|
|
receive (``request.GET`` or ``request.POST``); relying on the combined
|
|
|
|
``request.REQUEST`` dictionary can mask the origin of incoming data.
|
|
|
|
|
2008-09-02 16:27:47 +00:00
|
|
|
Accessing ``HTTPResponse`` headers
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
``django.http.HttpResponse.headers`` has been renamed to ``_headers`` and
|
2010-11-28 17:49:43 +00:00
|
|
|
:class:`~django.http.HttpResponse` now supports containment checking directly.
|
|
|
|
So use ``if header in response:`` instead of ``if header in response.headers:``.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Generic relations
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Generic relations have been moved out of core
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The generic relation classes -- ``GenericForeignKey`` and ``GenericRelation``
|
|
|
|
-- have moved into the :mod:`django.contrib.contenttypes` module.
|
|
|
|
|
|
|
|
Testing
|
|
|
|
-------
|
|
|
|
|
|
|
|
:meth:`django.test.Client.login` has changed
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Old (0.96)::
|
|
|
|
|
|
|
|
from django.test import Client
|
|
|
|
c = Client()
|
|
|
|
c.login('/path/to/login','myuser','mypassword')
|
|
|
|
|
|
|
|
New (1.0)::
|
|
|
|
|
|
|
|
# ... same as above, but then:
|
|
|
|
c.login(username='myuser', password='mypassword')
|
|
|
|
|
|
|
|
Management commands
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Running management commands from your code
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2010-11-28 17:49:43 +00:00
|
|
|
:mod:`django.core.management` has been greatly refactored.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
Calls to management services in your code now need to use
|
2008-09-02 16:27:47 +00:00
|
|
|
``call_command``. For example, if you have some test code that calls flush and
|
|
|
|
load_data::
|
|
|
|
|
|
|
|
from django.core import management
|
|
|
|
management.flush(verbosity=0, interactive=False)
|
|
|
|
management.load_data(['test_data'], verbosity=0)
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
...you'll need to change this code to read::
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
from django.core import management
|
|
|
|
management.call_command('flush', verbosity=0, interactive=False)
|
|
|
|
management.call_command('loaddata', 'test_data', verbosity=0)
|
|
|
|
|
2009-06-18 13:35:36 +00:00
|
|
|
Subcommands must now precede options
|
2008-09-02 16:27:47 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
``django-admin.py`` and ``manage.py`` now require subcommands to precede
|
|
|
|
options. So:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ django-admin.py --settings=foo.bar runserver
|
|
|
|
|
2008-09-05 00:48:34 +00:00
|
|
|
...no longer works and should be changed to:
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ django-admin.py runserver --settings=foo.bar
|
|
|
|
|
|
|
|
Syndication
|
|
|
|
-----------
|
|
|
|
|
|
|
|
``Feed.__init__`` has changed
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-07 19:15:41 +00:00
|
|
|
The ``__init__()`` method of the syndication framework's ``Feed`` class now
|
|
|
|
takes an ``HttpRequest`` object as its second parameter, instead of the feed's
|
2008-09-02 16:27:47 +00:00
|
|
|
URL. This allows the syndication framework to work without requiring the sites
|
2008-09-07 19:15:41 +00:00
|
|
|
framework. This only affects code that subclasses ``Feed`` and overrides the
|
2008-09-02 16:27:47 +00:00
|
|
|
``__init__()`` method, and code that calls ``Feed.__init__()`` directly.
|
|
|
|
|
|
|
|
Data structures
|
|
|
|
---------------
|
|
|
|
|
|
|
|
``SortedDictFromList`` is gone
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-07 19:15:41 +00:00
|
|
|
``django.newforms.forms.SortedDictFromList`` was removed.
|
|
|
|
:class:`django.utils.datastructures.SortedDict` can now be instantiated with
|
2008-09-02 16:27:47 +00:00
|
|
|
a sequence of tuples.
|
|
|
|
|
|
|
|
To update your code:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
1. Use :class:`django.utils.datastructures.SortedDict` wherever you were
|
|
|
|
using ``django.newforms.forms.SortedDictFromList``.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
2. Because :meth:`django.utils.datastructures.SortedDict.copy` doesn't
|
|
|
|
return a deepcopy as ``SortedDictFromList.copy()`` did, you will need
|
|
|
|
to update your code if you were relying on a deepcopy. Do this by using
|
|
|
|
``copy.deepcopy`` directly.
|
2008-09-02 16:27:47 +00:00
|
|
|
|
|
|
|
Database backend functions
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Database backend functions have been renamed
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Almost *all* of the database backend-level functions have been renamed and/or
|
|
|
|
relocated. None of these were documented, but you'll need to change your code
|
|
|
|
if you're using any of these functions, all of which are in :mod:`django.db`:
|
|
|
|
|
|
|
|
======================================= ===================================================
|
|
|
|
Old (0.96) New (1.0)
|
|
|
|
======================================= ===================================================
|
|
|
|
``backend.get_autoinc_sql`` ``connection.ops.autoinc_sql``
|
|
|
|
``backend.get_date_extract_sql`` ``connection.ops.date_extract_sql``
|
|
|
|
``backend.get_date_trunc_sql`` ``connection.ops.date_trunc_sql``
|
|
|
|
``backend.get_datetime_cast_sql`` ``connection.ops.datetime_cast_sql``
|
|
|
|
``backend.get_deferrable_sql`` ``connection.ops.deferrable_sql``
|
|
|
|
``backend.get_drop_foreignkey_sql`` ``connection.ops.drop_foreignkey_sql``
|
|
|
|
``backend.get_fulltext_search_sql`` ``connection.ops.fulltext_search_sql``
|
|
|
|
``backend.get_last_insert_id`` ``connection.ops.last_insert_id``
|
|
|
|
``backend.get_limit_offset_sql`` ``connection.ops.limit_offset_sql``
|
|
|
|
``backend.get_max_name_length`` ``connection.ops.max_name_length``
|
|
|
|
``backend.get_pk_default_value`` ``connection.ops.pk_default_value``
|
|
|
|
``backend.get_random_function_sql`` ``connection.ops.random_function_sql``
|
|
|
|
``backend.get_sql_flush`` ``connection.ops.sql_flush``
|
|
|
|
``backend.get_sql_sequence_reset`` ``connection.ops.sequence_reset_sql``
|
|
|
|
``backend.get_start_transaction_sql`` ``connection.ops.start_transaction_sql``
|
|
|
|
``backend.get_tablespace_sql`` ``connection.ops.tablespace_sql``
|
|
|
|
``backend.quote_name`` ``connection.ops.quote_name``
|
|
|
|
``backend.get_query_set_class`` ``connection.ops.query_set_class``
|
|
|
|
``backend.get_field_cast_sql`` ``connection.ops.field_cast_sql``
|
|
|
|
``backend.get_drop_sequence`` ``connection.ops.drop_sequence_sql``
|
|
|
|
``backend.OPERATOR_MAPPING`` ``connection.operators``
|
|
|
|
``backend.allows_group_by_ordinal`` ``connection.features.allows_group_by_ordinal``
|
|
|
|
``backend.allows_unique_and_pk`` ``connection.features.allows_unique_and_pk``
|
|
|
|
``backend.autoindexes_primary_keys`` ``connection.features.autoindexes_primary_keys``
|
|
|
|
``backend.needs_datetime_string_cast`` ``connection.features.needs_datetime_string_cast``
|
|
|
|
``backend.needs_upper_for_iops`` ``connection.features.needs_upper_for_iops``
|
|
|
|
``backend.supports_constraints`` ``connection.features.supports_constraints``
|
|
|
|
``backend.supports_tablespaces`` ``connection.features.supports_tablespaces``
|
|
|
|
``backend.uses_case_insensitive_names`` ``connection.features.uses_case_insensitive_names``
|
|
|
|
``backend.uses_custom_queryset`` ``connection.features.uses_custom_queryset``
|
|
|
|
======================================= ===================================================
|
|
|
|
|