2 Utilities for Django models.
5 from django.utils.translation import string_concat
8 def filtered_model(name, model, field, value, verbose_extra=None):
9 """Creates a proxy model filtering objects by a field."""
10 verbose_extra = verbose_extra or value
13 app_label = model._meta.app_label
14 verbose_name = string_concat(model._meta.verbose_name,
16 verbose_name_plural = string_concat(model._meta.verbose_name_plural,
19 def save(self, *args, **kwargs):
20 if not getattr(self, field):
21 setattr(self, field, value)
22 return model.save(self, *args, **kwargs)
24 attrs = {'__module__': '', 'Meta': Meta, 'save': save}
25 return type(name, (model,), attrs)