Version 10 (modified by 19 years ago) ( diff ) | ,
---|
Django model syntax change
As of [549], Django's model syntax has changed. If you're using models that use old (pre-[549]) syntax, you'll need to convert them according to the these instructions.
What changed
- Fields are now attributes of the model class, rather than members of a
fields
list. - Meta information (anything that's NOT a field, such as
ordering
,admin
,unique_together
, etc.) now goes in an inner class, calledMETA
(note the all caps). This class doesn't have a parent class. - To manually specify a field's database column name, use
db_column
as an argument to*Field()
. - When instantiating an object, you can now pass in many-to-one related object instances. Example:
p = polls.get_object(pk=1); choice = choices.Choice(poll=p, choice="Hello", votes=0)
. Note thatchoices.choice(poll_id=p.id, ...)
still works. - Each field is required to have an explicit name -- even
ForeignKey
s,ManyToManyField
s andOneToOneFields
. This solves the problem of "How do I refer to my field from within admin.fields?" rel_name
is no longer used forForeignKey
s. If your model has more than oneForeignKey
to the same foreign model, differentiate the fields using the field name, notrel_name
. See Relating a model to another model more than once for an example.rel_name
is no longer used forManyToManyField
s. If your model has more than oneManyToManyField
to the same foreign model, differentiate the fields using the field name, notrel_name
. Also, give both of theManyToManyField
s asingular
attribute, which defines the name of the related object in singular format. (This is an obscure case, but it's included here for completeness.)- Syntax for subclassing models (
add_fields
,remove_fields
) has changed. Seetests/testapp/models/subclassing.py
for examples.
Examples
Old syntax example
class Foo(meta.Model): fields = ( meta.CharField('first_name', "The person's first name", maxlength=30), meta.CharField('last_name', maxlength=30), meta.ForeignKey(Bar), meta.ManyToManyField(Sites), ) ordering = ('-bar_id',) admin = meta.Admin( fields = ( (None, {'fields': ('first_name', 'last_name', 'bar_id', 'sites')}), ), ) def __repr__(self): return self.first_name
New syntax example
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
Notes:
bar
andsites
now have explicit names, andadmin.fields
was changed to usebar
instead ofbar_id
.ordering
was also changed to use the explicit namebar
instead ofbar_id
.bar_id
isn't used anywhere anymore; always use the explicit field name as defined in the model."sites"
is the explicit field name for theSites
many-to-many relationship. Previously, Django calculated this automatically by using themodule_name
of the related model by default, orrel_name
if it was given.- The
verbose_name
is now an optional first argument toField
s. ForForeignKey
s,ManyToManyField
s andOneToOneField
s, use a named argument:verbose_name="foo"
. - Don't forget to remove the commas after each
Field
, because they're class attributes instead of tuple elements now. - Custom methods (such as
__repr__()
here) still go at the class level. They haven't changed.
Note:
See TracWiki
for help on using the wiki.