1 from django.utils.translation import string_concat
3 def filtered_model(name, model, field, value, verbose_extra=None):
4 """Creates a proxy model filtering objects by a field."""
5 verbose_extra = verbose_extra or value
8 app_label = model._meta.app_label
9 verbose_name = string_concat(model._meta.verbose_name,
11 verbose_name_plural = string_concat(model._meta.verbose_name_plural,
14 def save(self, *args, **kwargs):
15 if not getattr(self, field):
16 setattr(self, field, value)
17 return model.save(self, *args, **kwargs)
19 attrs = {'__module__': '', 'Meta': Meta, 'save': save}
20 return type(name, (model,), attrs)