| 108 | |
| 109 | == Changed model syntax == |
| 110 | |
| 111 | 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 following instructions. |
| 112 | |
| 113 | === What changed === |
| 114 | |
| 115 | * Fields are now attributes of the model class, rather than members of a {{{fields}}} list. |
| 116 | * 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. |
| 117 | * 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?" |
| 118 | * {{{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. |
| 119 | * {{{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.) |
| 120 | |
| 121 | === Examples === |
| 122 | |
| 123 | Old syntax example: |
| 124 | |
| 125 | {{{ |
| 126 | #!python |
| 127 | class Foo(meta.Model): |
| 128 | fields = ( |
| 129 | meta.CharField('first_name', maxlength=30), |
| 130 | meta.CharField('last_name', maxlength=30), |
| 131 | meta.ForeignKey(Bar), |
| 132 | meta.ManyToManyField(Sites), |
| 133 | ) |
| 134 | ordering = ('-bar_id',) |
| 135 | admin = meta.Admin( |
| 136 | fields = ( |
| 137 | (None, {'fields': ('first_name', 'last_name', 'bar_id', 'sites')}), |
| 138 | ), |
| 139 | ) |
| 140 | }}} |
| 141 | |
| 142 | New syntax example: |
| 143 | |
| 144 | {{{ |
| 145 | #!python |
| 146 | class Foo(meta.Model): |
| 147 | first_name = meta.CharField('first_name', maxlength=30) |
| 148 | last_name = meta.CharField('last_name', maxlength=30) |
| 149 | bar = meta.ForeignKey(Bar) |
| 150 | sites = meta.ManyToManyField(Sites) |
| 151 | class META: |
| 152 | ordering = ('-bar',) |
| 153 | admin = meta.Admin( |
| 154 | fields = ( |
| 155 | (None, {'fields': ('first_name', 'last_name', 'bar', 'sites')}), |
| 156 | ), |
| 157 | ) |
| 158 | }}} |
| 159 | |
| 160 | Notes: |
| 161 | |
| 162 | * {{{bar}}} and {{{sites}}} now have explicit names, and {{{admin.fields}}} was changed to use {{{bar}}} instead of {{{bar_id}}}. |
| 163 | * {{{ordering}}} was also changed to use the explicit name {{{bar}}} instead of {{{bar_id}}}. |
| 164 | * Don't forget to remove the commas after each {{{Field}}}, because they're class attributes instead of list elements now. |