323 | | ==== Next Steps |
324 | | - Feedback |
325 | | - Code cleanup and code documentation |
326 | | - Django manual documentation |
327 | | - Decide deprecaction strategy |
328 | | - More benchmarking |
329 | | - Merge |
| 323 | |
| 324 | == Further API improvements |
| 325 | |
| 326 | After a number of iterations, and many discussions on the Mailing List ( https://groups.google.com/forum/#!topic/django-developers/XfMUjK59Sls) we have come up with the new, simplified, API. |
| 327 | |
| 328 | === Model._meta API functions |
| 329 | |
| 330 | {{{ |
| 331 | def get_fields(self, forward=True, reverse=False) |
| 332 | }}} |
| 333 | |
| 334 | We have made a distinction between fields that are on the model (forward) and fields that point to the model (reverse). Compared to the previous API we are not making a distinction on if a field is a M2M, Foreign Key, or normal "data" field. This allows greater flexibility in the future and only makes one clear separation of field types. API consumers will be able to do further filtering using the field_flags (see below) |
| 335 | |
| 336 | {{{ |
| 337 | def get_field(self, field_name) |
| 338 | }}} |
| 339 | |
| 340 | We have dropped the include_relations flag because it is no longer required. The API is still 100% backwards compatible. A field can be a reverse or a forward field and can be accessed using 1 or more names (ex. {"user", "user_id"}). |
| 341 | |
| 342 | |
| 343 | === Model._meta API accessors |
| 344 | |
| 345 | ===== _meta.fields |
| 346 | Returns all forward fields that may have relations and store only 1 single value. |
| 347 | |
| 348 | ===== _meta.many_to_many |
| 349 | Returns all forward fields that may have relations and store multiple values. |
| 350 | |
| 351 | ===== _meta.concrete_fields |
| 352 | Returns all forward concrete fields that may have relations and store only 1 single value. |
| 353 | |
| 354 | ===== _meta.local_concrete_fields |
| 355 | Returns all forward concrete fields, defined on the current model, that may have relations and store only 1 single value. |
| 356 | |
| 357 | ===== _meta.related_objects |
| 358 | Returns all the reverse objects pointing to the current model. |
| 359 | |
| 360 | |
| 361 | |
| 362 | This API has remained the same, but the newer implementation is more efficient. |
| 363 | |
| 364 | |
| 365 | |
| 366 | ==== Use cached_properties when possible |
| 367 | Function calls are expensive in Python, All sensible attributes with no arguments have been transformed into cached_properties. |
| 368 | A cached property is a read-only property that is calculated on demand and automatically cached. If the value has already been calculated, the cached value is returned. Cached properties avoid a new stack and are used for fast-access to fields, concrete_fields, |
| 369 | local_concrete_fields, many_to_many, field_names |
| 370 | |
| 371 | |
| 372 | |