pytoolbox.django.utils.collections module

class pytoolbox.django.utils.collections.FieldsToValuesLookupDict(name, translations=None)[source]

Bases: object

Global registry for mapping X class fields to W values.

  • X can be a Model, a (Model)Form, a REST Framework Serializer, …
  • Y can be the fields help texts or verbose names, or the number 42.

Strange idea? Isn’t it?

Here is a short example as an appetizer. Suppose you want to define your application’s help texts into a centralized registry, for keeping your wording DRY. And suppose you have some models like this:

>> class Media(models.Model): .. url = models.URLField()

>> class File(models.Model): .. url = models.URLField()

And you instantiate this class with:

>> help_texts = FieldsLookupDict({‘Media.url’: ‘The media asset ingest URL’, ‘url’: ‘An URL’})

Then, you can lookup for the help text of a field like this:

>> help_texts[(Media, ‘url’)] The media asset ingest URL

>> help_texts[(File, ‘url’)] An URL

The value returned will be the first matching to the following keys:

  1. ‘<cls.__name__>.<field_name>’
  2. ‘<field_name>’

If given class have a _meta or Meta (“meta”) attribute with a model attribute, then the following keys are tried:

  1. ‘<cls.__name__>.<field_name>’
  2. ‘<cls._meta.model>.<field_name>’
  3. ‘<field_name>’
__init__(name, translations=None)[source]

Initialize self. See help(type(self)) for accurate signature.