| 1 | == Django model syntax change == |
| 2 | |
| 3 | As of ____, Django's model syntax has changed. If you're using models that use old (pre-____) syntax, you'll need to convert them according to the instructions on ModelSyntaxChangeInstructions. |
| 4 | |
| 5 | === What changed === |
| 6 | |
| 7 | * Fields are now attributes of the model class, rather than members of a {{{fields}}} list. |
| 8 | * Meta information (anything that's NOT a field, such as {{{ordering}}}, {{{admin}}}, {{{unique_together}}}, etc.) now goes in an inner class, called {{{META}}} (note the all caps). This class doesn't have a parent class. |
| 9 | * Each field is required to have an explicit name -- even {{{ForeignKey}}}s, {{{ManyToManyField}}}s and {{{OneToOneFields}}}. This solves the problem of "How do I refer to my field from within admin.fields?" |
| 10 | * {{{rel_name}}} is no longer used for {{{ForeignKey}}}s. If your model has more than one {{{ForeignKey}}} to the same foreign model, differentiate the fields using the field name, not {{{rel_name}}}. See [http://www.djangoproject.com/documentation/models/m2o_recursive2/ Relating a model to another model more than once] for an example. |
| 11 | * {{{rel_name}}} is no longer used for {{{ManyToManyField}}}s. If your model has more than one {{{ManyToManyField}}} to the same foreign model, differentiate the fields using the field name, not {{{rel_name}}}. Also, give both of the {{{ManyToManyField}}}s a {{{singular}}} attribute, which defines the name of the related object in singular format. (This is an obscure case, but it's included here for completeness.) |
| 12 | |
| 13 | === Examples === |
| 14 | |
| 15 | Old syntax example: |
| 16 | |
| 17 | {{{ |
| 18 | #!python |
| 19 | class Foo(meta.Model): |
| 20 | fields = ( |
| 21 | meta.CharField('first_name', maxlength=30), |
| 22 | meta.CharField('last_name', maxlength=30), |
| 23 | meta.ForeignKey(Bar), |
| 24 | meta.ManyToManyField(Sites), |
| 25 | ) |
| 26 | ordering = ('-bar_id',) |
| 27 | admin = meta.Admin( |
| 28 | fields = ( |
| 29 | (None, {'fields': ('first_name', 'last_name', 'bar_id', 'sites')}), |
| 30 | ), |
| 31 | ) |
| 32 | }}} |
| 33 | |
| 34 | New syntax example: |
| 35 | |
| 36 | {{{ |
| 37 | #!python |
| 38 | class Foo(meta.Model): |
| 39 | first_name = meta.CharField('first_name', maxlength=30) |
| 40 | last_name = meta.CharField('last_name', maxlength=30) |
| 41 | bar = meta.ForeignKey(Bar) |
| 42 | sites = meta.ManyToManyField(Sites) |
| 43 | class META: |
| 44 | ordering = ('-bar',) |
| 45 | admin = meta.Admin( |
| 46 | fields = ( |
| 47 | (None, {'fields': ('first_name', 'last_name', 'bar', 'sites')}), |
| 48 | ), |
| 49 | ) |
| 50 | }}} |
| 51 | |
| 52 | Notes: |
| 53 | |
| 54 | * {{{bar}}} and {{{sites}}} now have explicit names, and {{{admin.fields}}} was changed to use {{{bar}}} instead of {{{bar_id}}}. |
| 55 | * {{{ordering}}} was also changed to use the explicit name {{{bar}}} instead of {{{bar_id}}}. |
| 56 | * Don't forget to remove the commas after each {{{Field}}}, because they're class attributes instead of list elements now. |