| | 125 | |
|---|
| | 126 | |
|---|
| | 127 | Extent labels |
|---|
| | 128 | ------------- |
|---|
| | 129 | |
|---|
| | 130 | By default, an extent has a singular and plural label based on the |
|---|
| | 131 | entity class name associated with the extent:: |
|---|
| | 132 | |
|---|
| | 133 | >>> from schevo.label import label, plural |
|---|
| | 134 | >>> t = DocTest(""" |
|---|
| | 135 | ... class GreatPerson(E.Entity): |
|---|
| | 136 | ... |
|---|
| | 137 | ... name = f.unicode() |
|---|
| | 138 | ... """) |
|---|
| | 139 | >>> print label(t.db.GreatPerson) |
|---|
| | 140 | Great Person |
|---|
| | 141 | >>> print plural(t.db.GreatPerson) |
|---|
| | 142 | Great Persons |
|---|
| | 143 | |
|---|
| | 144 | To override the singular and/or plural labels of an extent, assign |
|---|
| | 145 | values to the `_label` and/or `_plural` attributes of the associated |
|---|
| | 146 | entity class:: |
|---|
| | 147 | |
|---|
| | 148 | >>> from schevo.label import label, plural |
|---|
| | 149 | >>> t = DocTest(""" |
|---|
| | 150 | ... class GreatPerson(E.Entity): |
|---|
| | 151 | ... |
|---|
| | 152 | ... name = f.unicode() |
|---|
| | 153 | ... |
|---|
| | 154 | ... _label = 'Great PERSON' |
|---|
| | 155 | ... _plural = 'Great PEOPLE' |
|---|
| | 156 | ... """) |
|---|
| | 157 | >>> print label(t.db.GreatPerson) |
|---|
| | 158 | Great PERSON |
|---|
| | 159 | >>> print plural(t.db.GreatPerson) |
|---|
| | 160 | Great PEOPLE |
|---|
| | 197 | |
|---|
| | 198 | |
|---|
| | 199 | Entity labels |
|---|
| | 200 | ------------- |
|---|
| | 201 | |
|---|
| | 202 | The `label` of an entity instance is the same as the `unicode` |
|---|
| | 203 | representation of the entity instance. |
|---|
| | 204 | |
|---|
| | 205 | By default, the label is determined by the default key defined in the |
|---|
| | 206 | entity class:: |
|---|
| | 207 | |
|---|
| | 208 | >>> from schevo.label import label |
|---|
| | 209 | >>> t = DocTest(""" |
|---|
| | 210 | ... class State(E.Entity): |
|---|
| | 211 | ... |
|---|
| | 212 | ... name = f.unicode() |
|---|
| | 213 | ... abbreviation = f.unicode() |
|---|
| | 214 | ... |
|---|
| | 215 | ... _key(name) |
|---|
| | 216 | ... _key(abbreviation) |
|---|
| | 217 | ... |
|---|
| | 218 | ... _initial = [ |
|---|
| | 219 | ... ('Missouri', 'MO'), |
|---|
| | 220 | ... ] |
|---|
| | 221 | ... """) |
|---|
| | 222 | >>> missouri = t.db.State.findone(name='Missouri') |
|---|
| | 223 | >>> print label(missouri) |
|---|
| | 224 | Missouri |
|---|
| | 225 | |
|---|
| | 226 | If the key has more than one field, the values are separated by two |
|---|
| | 227 | colons:: |
|---|
| | 228 | |
|---|
| | 229 | >>> t = DocTest(""" |
|---|
| | 230 | ... class City(E.Entity): |
|---|
| | 231 | ... |
|---|
| | 232 | ... name = f.unicode() |
|---|
| | 233 | ... state = f.entity('State') |
|---|
| | 234 | ... |
|---|
| | 235 | ... _key(name, state) |
|---|
| | 236 | ... |
|---|
| | 237 | ... _initial = [ |
|---|
| | 238 | ... ('Monett', ('Missouri',)), |
|---|
| | 239 | ... ] |
|---|
| | 240 | ... |
|---|
| | 241 | ... class State(E.Entity): |
|---|
| | 242 | ... |
|---|
| | 243 | ... name = f.unicode() |
|---|
| | 244 | ... abbreviation = f.unicode() |
|---|
| | 245 | ... |
|---|
| | 246 | ... _key(name) |
|---|
| | 247 | ... _key(abbreviation) |
|---|
| | 248 | ... |
|---|
| | 249 | ... _initial = [ |
|---|
| | 250 | ... ('Missouri', 'MO'), |
|---|
| | 251 | ... ] |
|---|
| | 252 | ... """) |
|---|
| | 253 | >>> missouri = t.db.State.findone(name='Missouri') |
|---|
| | 254 | >>> monett = t.db.City.findone(name='Monett', state=missouri) |
|---|
| | 255 | >>> print label(monett) |
|---|
| | 256 | Monett :: Missouri |
|---|
| | 257 | |
|---|
| | 258 | Override the `__unicode__` method of an entity class to customize the |
|---|
| | 259 | label:: |
|---|
| | 260 | |
|---|
| | 261 | >>> t = DocTest(""" |
|---|
| | 262 | ... class City(E.Entity): |
|---|
| | 263 | ... |
|---|
| | 264 | ... name = f.unicode() |
|---|
| | 265 | ... state = f.entity('State') |
|---|
| | 266 | ... |
|---|
| | 267 | ... _key(name, state) |
|---|
| | 268 | ... |
|---|
| | 269 | ... def __unicode__(self): |
|---|
| | 270 | ... return u'%s, %s' % (self.name, self.state.abbreviation) |
|---|
| | 271 | ... |
|---|
| | 272 | ... _initial = [ |
|---|
| | 273 | ... ('Monett', ('Missouri',)), |
|---|
| | 274 | ... ] |
|---|
| | 275 | ... |
|---|
| | 276 | ... class State(E.Entity): |
|---|
| | 277 | ... |
|---|
| | 278 | ... name = f.unicode() |
|---|
| | 279 | ... abbreviation = f.unicode() |
|---|
| | 280 | ... |
|---|
| | 281 | ... _key(name) |
|---|
| | 282 | ... _key(abbreviation) |
|---|
| | 283 | ... |
|---|
| | 284 | ... _initial = [ |
|---|
| | 285 | ... ('Missouri', 'MO'), |
|---|
| | 286 | ... ] |
|---|
| | 287 | ... """) |
|---|
| | 288 | >>> missouri = t.db.State.findone(name='Missouri') |
|---|
| | 289 | >>> monett = t.db.City.findone(name='Monett', state=missouri) |
|---|
| | 290 | >>> print label(monett) |
|---|
| | 291 | Monett, MO |
|---|