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.
SmartModel¶
chamber.models.SmartModel improved django Model class with several features that simplify development of complex applications
-
class
chamber.models.SmartModel¶ -
dispatchers¶ List of defined pre or post save dispatchers. More obout it will find _dispatchers
-
has_changed¶ Returns
TrueorFalsedepending on whether instance field values were changed
-
initial_values¶ Returns initial values of the object from loading instance from database. It should represent actual state of the object in the database
-
is_adding¶ Returns
TrueorFalsedepending on whether model instance will be inserting or updating in the database
-
is_changing¶ Returns
TrueorFalsedepending on whether model instance will be updating or inserting in the database
-
initial_values Returns dict initial values of all instance fields that were loaded from the database. If object is not stored in the database the values will be
Unknownwhich is similar object toNonevalue
-
changed_fields¶ Returns
ChangedFieldsinstance that contains information which fields have been changed and how
-
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(change, changed_fields, *args, **kwargs)¶ Method that is called before saving instance. You can here change instance structure or call some operations before saving object. Parameter
changehas True value if model instance existed before save was called, otherwise False. Parameterchanged_fieldscontains name of changed fields and its original and current values. Changes in the method body have no effect onchanged_fieldsproperty.
-
_post_save(change, changed_fields, *args, **kwargs)¶ Method that is called after saving instance. You can here change instance structure or call some operations after saving object. Parameter
changehas True value if model instance existed before save was called, otherwise False. Parameterchanged_fieldscontains name of changed fields and its original and current values. Changes in the method body have no effect onchanged_fieldsproperty.
-
_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 sent in
changed_fields
-
change_and_save(update_only_changed_fields=False, **changed_fields)¶ Update instance field values with values sent in
changed_fieldsand finally instance is saved. If you want to update only changed fields in the database you can use parameterupdate_only_changed_fieldsto achieve it
-
full_clean(exclude=None, *args, **kwargs)¶ The original django full_clean method is improved with calling extra clean_{field} methods on the model (it is similar to django forms clean methods:
class CustomModel(SmartModel): name = models.CharField(max_length=100) def clean_name(self): if len(self.name) >= 10: raise ValidationError('name must be lower than 10')
The method
clean_namewill be automatically called when thefull_cleanmethod is triggered and ValidationError will be added to the field error.
-
get_locked_instance()¶ The method returns the new instance of the self object which is locked in the database with
select_for_update. Method must be used in the django atomic block.
-
SmartAuditModel¶
SmartModel with two extra fields created_at and changed_at to log when the model was created or updated.
-
class
chamber.models.SmartAuditModel¶ -
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.
-
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 isFalse
-
is_cleaned_post_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_post_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
The default configuration for all smart models can be defined in your settings with setting CHAMBER_SMART_MODEL_ATTRIBUTES:
CHAMBER_SMART_MODEL_ATTRIBUTES = {
'is_cleaned_pre_save': True,
'is_cleaned_post_save': False,
'is_cleaned_pre_delete': False,
'is_cleaned_post_delete': False,
'is_save_atomic': False,
'is_delete_atomic': False,
}
Unknown¶
chamber.models.Unknown is singleton that is used for initial field values if object is not stored in the database. The purpose is distinguish None value from Unknown value because None can be stored in the database as a normal value. The Unknown behavior is same as None.
ChangedFields¶
chamber.models.ChangedFields is dict-like class that is used for storing which fields of model instance were changed
>>> user = User.objects.get(last_name='Gaul')
>>> user.last_name = 'Goul'
>>> 'last_name' in user.changed_fields
True
>>> 'first_name' in user.changed_fields
False
>>> user.changed_fields.has_any_key('first_name', 'last_name')
True
>>> user.changed_fields['last_name'].initial
'Gaul'
>>> user.changed_fields['last_name'].current
'Goul'
>>> user.changed_fields.keys()
['last_name']
-
class
ChangedFields¶ -
initial_values¶ Return initial values of the model instance
-
current_values¶ Return current values of the model instance
-
diff¶ Return only changed values of the model instance
-
has_key(k)¶ Return if field
kwas changed
-
has_any_key(*keys)¶ Return if any of the fields
keyswere changed
-
keys()¶ Return all names of the changed fields
-
values()¶ Return list of
ValueChangeof all changed fields. WhereValueChangeis POJO object that contains field value before changed and value after change (initialandcurrentattribute)
-
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(update_only_changed_fields=False, **changed_fields)¶ Changes selected fields on the selected queryset and saves it and returns changed objects in the queryset. Difference from update is that there is called save method on the instance, but it is slower. If you want to update only changed fields in the database you can use parameter
update_only_changed_fieldsto achieve it
-
first(**field_names)¶ Method is only shortcut to
Model.objects.order_by('field_name').first(). With this method you can useModel.objects.first('field_name')
-
last(**field_names)¶ Method is only shortcut to
Model.objects.order_by('field_name').last(). With this method you can useModel.objects.last('field_name')
-