Models¶
Model Fields¶
-
class
chamber.models.fields.DecimalField¶
Same as django.db.models.DecimalField but with additional attributes. Uses chamber.forms.fields.DecimalField as the default form field.
min¶A minimal value of the field. An appropriate
MinValueValidatoris added automatically. The HTMLmincomponent is added to the form field.
max¶A maximal value of the field. An appropriate
MaxValueValidatoris added automatically. The HTMLmaxcomponent is added to the form field.
step¶The step between values that is set on the form field.
-
class
chamber.models.fields.RestrictedFileFieldMixin¶
Same as FileField, but you can specify:
-
class
chamber.models.fields.FileField¶
The django.db.models.FileField with RestrictedFileFieldMixin options.
-
class
chamber.models.fields.ImageField¶
sorl.thumbnail.ImageField with fallback to django.db.models.ImageField when sorl is not installed. Supports RestrictedFileFieldMixin options.
-
class
chamber.models.fields.CharNullField¶
django.db.models.CharField that stores NULL but returns '' .
-
class
chamber.models.fields.PriceField¶
chamber.models.fields.DecimalField with defaults:
-
class
chamber.models.fields.PositivePriceField¶
chamber.models.fields.PriceField with a default django.core.validators.MinValueValidator set to 0.00.
-
class
chamber.models.fields.SouthMixin¶
Mixin for automatic South migration of custom model fields.
SmartModel¶
chamber.models.SmartModel improved django Model class with several features that simplify development of complex applications
-
class
chamber.models.SmartModel¶ -
created_at¶ Because our experience has shown us that datetime of creation is very useful this field
django.models.DateTimeFieldwithauto_add_noset toTrueis added to every model that inherits fromSmartModel
-
changed_at¶ This model field is same case as
created_atwith the difference that there is usedauto_now=Truetherefore every date and time of change is stored here.
-
dispatchers¶ List of defined pre or post save dispatchers. More obout it will find _dispatchers
-
clean_<field_name>() Like a django form field you can use your own method named by field name for cleaning input value. You can too raise
ValidationErrorif input value is invalid
-
_pre_save()¶ Method that is called before saving instance. You can here change instance structure or call some operations before saving object
-
_post_save()¶ Method that is called after saving instance. You can here change instance structure or call some operations after saving object
-
_pre_delete()¶ Method that is called before removing instance. You can here change instance structure or call some operations before removing object
-
_post_delete()¶ Method that is called after removing instance. You can here change instance structure or call some operations after removing object
-
refresh_from_db()¶ There is used implementation from django
refresh_from_dbmethod with small change that method returns refreshed instance
-
change(**changed_fields)¶ Update instance field values with values send in
changed_fields
-
change_and_save(**changed_fields)¶ Update instance field values with values send in
changed_fieldsand finally instance is saved
-
SmartMeta¶
SmartMeta similar like django meta is defined inside SmartModel and is accessible via _smart_meta attribute. Its purpose is define default SmartModel behavior.
-
class
SmartMeta¶ -
is_cleaned_pre_save¶ Defines if
SmartModelwill be automatically validated before saving. Default value isTrue
-
is_cleaned_pre_save Defines if
SmartModelwill be automatically validated after saving. Default value isFalse
-
is_cleaned_pre_delete¶ Defines if
SmartModelwill be automatically validated before removing. Default value isFalse
-
is_cleaned_pre_delete Defines if
SmartModelwill be automatically validated after removing. Default value isFalse
-
is_save_atomic¶ Defines if
SmartModelwill be saved in transaction atomic blockFalse
-
is_delete_atomic¶ Defines if
SmartModelwill be removed in transaction atomic blockFalse
-
class SmartModelWithMeta(SmartModel):
class SmartMeta:
is_cleaned_pre_save = True
is_cleaned_pre_delete = True
SmartQuerySet¶
SmartModel uses a modified QuerySet by default with some convenience filters.
If you are overriding model manager of a SmartModel, you should
incorporate SmartQuerySet in order not to lose its benefits and to
follow the Rule of the Least Surprise (everyone using your SmartModel
will assume the custom filters to be there).
- If the manager is created using the
QuerySet.as_manager()method, your custom queryset should subclassSmartQuerySetinstead the one from Django. - If you have a new manager created by subclassing
models.Managerfrom Django, you should override theget_querysetmethod as shown in Django docs `here`_.
-
class
chamber.models.SmartQuerySet¶ -
fast_distinct()¶ Returns same result as regular
distinct()but is much faster especially in PostgreSQL which performs distinct on all DB columns. The optimization is achieved by doing a second query and the__inoperator. If you have querysetqsofMyModelthenfast_distinct()equals to callingMyModel.objects.filter(pk__in=qs.values_list('pk', flat=True))
-
change_and_save(**changed_fields)¶ Change selected fields on the selected queryset and saves it, finnaly is returned changed objects in the queryset. Difference from update is that there is called save method on the instance, but it is slower.
-