IMPORTANT: Django model syntax is changing

22 views
Skip to first unread message

Adrian Holovaty

unread,
Aug 25, 2005, 6:34:01 PM8/25/05
to django...@googlegroups.com, django-d...@googlegroups.com
All,

By popular demand, I'm about to change Django's model syntax to use
"fieldname = FieldClass" instead of "FieldClass('fieldname')". This
has been a longstanding ticket --
http://code.djangoproject.com/ticket/122.

This change is BACKWARDS-INCOMPATIBLE, so don't "svn update" your code
until you've changed your models. Old model syntax will not work.

I apologize for the backwards-incompatibility, but this is still
unofficial software. ;-) Once we reach 1.0 -- which is much closer now
that the model syntax is changed -- we'll be very dedicated to
backwards-compatibility.

I can't think of any other backwards-incompatible changes that we're
planning before 1.0 (knock on wood). If this isn't the last one,
though, it's at least the last *major* one.

Here's a quick example of the new syntax:

class Foo(meta.Model):
first_name = meta.CharField("The person's first name", maxlength=30)
last_name = meta.CharField(maxlength=30)
bar = meta.ForeignKey(Bar)
sites = meta.ManyToManyField(Sites)
class META:
ordering = ('-bar',)
admin = meta.Admin(
fields = (
(None, {'fields': ('first_name', 'last_name', 'bar', 'sites')}),
),
)

def __repr__(self):
return self.first_name

Note that all non-field metadata goes in an inner class called "META".

Also note that ForeignKeys, ManyToManyFields and OneToOneFields are
now required to have an explicit name (in the example, that's "bar"
and "sites"). Before, field names for FK/MTM/OTO fields were optional
(as a shortcut), and that caused people confusion -- they didn't know
how to refer to fields within "admin.fields". Now it's explicit.

This change should be quite stable. I've been testing it for the past
two days in our production setting at World Online. Thanks to that, I
was able to iron out bugs as they came up. Also, I've updated every
one of the model unit tests (and, hence, the model examples at
http://www.djangoproject.com/documentation/models/ ) to use the new
syntax, and all tests pass.

Here's what you need to know to change your models:

* Full documentation on the change
http://code.djangoproject.com/wiki/ModelSyntaxChangeInstructions

* A screencast that goes through how to convert a model
http://www.djangoproject.com/screencasts/model_syntax_change/

Please send a note to the django-users mailing list or stop by IRC if
you have any problems.

I've gone through the documentation and attempted to update it to use
the new syntax. But I had to have missed *something*. If you're up for
it, please take a look through our docs
(http://www.djangoproject.com/documentation/) and let us know, by
filing a ticket, if you find any bugs.

Finally, I just want to publicly thank mmarshall, rmunn, garthk, hugo,
manuzhai and all the other great people who have contributed patches,
ideas and passionate discussion to this change. This is the first
really big community-driven improvement to Django, and it's been
awesome, for me personally, to see it come to fruition. Thanks, guys!

Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Milton Waddams

unread,
Aug 25, 2005, 10:18:04 PM8/25/05
to django-d...@googlegroups.com
It wouldn't work for me tb below, the admin for Site
(/usr/lib/python2.3/site-packages/django/models/core.py) needed to be
indented.

Unhandled exception in thread started by <function inner_run at 0x403ce224>
Traceback (most recent call last):
File "/usr/lib/python2.3/site-packages/django/core/management.py",
line 560, in inner_run
validate()
File "/usr/lib/python2.3/site-packages/django/core/management.py",
line 497, in validate
import django.models
File "/usr/lib/python2.3/site-packages/django/models/__init__.py",
line 13, in ?
modules = meta.get_installed_model_modules(__all__)
File "/usr/lib/python2.3/site-packages/django/core/meta/__init__.py",
line 106, in get_installed_model_modules
_installed_modules_cache.append(__import__('django.models.%s' %
submodule, '', '', ['']))
File "/usr/lib/python2.3/site-packages/django/models/auth.py", line 2, in ?
from django.models import core
File "/usr/lib/python2.3/site-packages/django/models/core.py", line 3, in ?
class Site(meta.Model):
File "/usr/lib/python2.3/site-packages/django/core/meta/__init__.py",
line 490, in __new__
assert callable(v), "%r is an invalid model parameter." % k
AssertionError: 'admin' is an invalid model parameter.

Adrian Holovaty

unread,
Aug 25, 2005, 11:40:44 PM8/25/05
to django-d...@googlegroups.com
On 8/25/05, Milton Waddams <ilikep...@gmail.com> wrote:
> It wouldn't work for me tb below, the admin for Site
> (/usr/lib/python2.3/site-packages/django/models/core.py) needed to be
> indented.

core.Site doesn't *have* an admin in the standard Django distribution,
so that's something you must've added yourself.

Milton Waddams

unread,
Aug 26, 2005, 6:39:57 AM8/26/05
to django-d...@googlegroups.com
must've been :)

sorry about that
Reply all
Reply to author
Forward
0 new messages