Behaviors¶
Usage Notes¶
Because behaviors are ultimately implemented using a special metaclass, any model inheritance involving behaviors must come before any other parent class which inherits from Django’s built in metaclass.
Example Correct and Incorrect Usage:
class MyBaseModel(models.Model):
pass
# Incorrect Usage
class MyChildModel(MyBaseModel, Timestampable):
pass
# Correct Usage
class MyChildModel(Timestampable, MyBaseModel):
pass
Built in Behaviors¶
-
class
fusionbox.behaviors.
Behavior
(*args, **kwargs)[source]¶ Base class for all Behaviors
Behaviors are implemented through model inheritance, and support multi-inheritance as well. Each behavior adds a set of default fields and/or methods to the model. Field names can be customized like example B.
EXAMPLE A:
class MyModel(FooBehavior): pass
MyModel
will have whatever fieldsFooBehavior
adds with default field names.EXAMPLE B:
class MyModel(FooBehavior): class FooBehavior: bar = 'qux' baz = 'quux'
MyModel
will have the fields fromFooBehavior
added, but the field names will be ‘qux’ and ‘quux’ respectively.EXAMPLE C:
class MyModel(FooBehavior, BarBehavior): pass
MyModel
will have the fields from bothFooBehavior
andBarBehavior
, each with default field names. To customizing field names can be done just like it was in example B.
-
fusionbox.behaviors.
ManagedQuerySet
¶ alias of
QuerySetManagerModel
-
class
fusionbox.behaviors.
Publishable
(*args, **kwargs)[source]¶ Base class for adding publishable behavior to a model.
- Added Fields:
- Field 1:
- field: DateTimeField(default=datetime.datetime.now, help_text=’Selecting a future date will automatically publish to the live site on that date.’) description: The date that the model instance will be made available to the PublishableManager’s query set default_name: publish_at
- Field 2:
- field: DateTimeField(default=datetime.datetime.now, help_text=’Selecting a future date will automatically publish to the live site on that date.’) description: setting to False will automatically draft the instance, making it unavailable to the PublishableManager’s query set default_name: is_published
- Added Managers:
- PublishableManager:
description: overwritten get_queryset() function to only fetch published instances. name: published usage:
class Blog(Publishable): ...
all_blogs = Blog.objects.all() published_blogs = Blog.published.all()
-
class
fusionbox.behaviors.
QuerySetManagerModel
(*args, **kwargs)[source]¶ This behavior is meant to be used in conjunction with
fusionbox.db.models.QuerySetManager
A class which inherits from this class will any inner QuerySet classes found in the mro merged into a single class.
Given the following Parent class:
class Parent(models.Model): class QuerySet(QuerySet): def get_active(self): ...
The following two Child classes are equivalent:
class Child(Parent): class QuerySet(Parent.QuerySet): def get_inactive(self): ... class Child(QuerySetManagerModel, Parent): class QuerySet(QuerySet): def get_inactive(self): ...
-
class
fusionbox.behaviors.
SEO
(*args, **kwargs)[source]¶ Base class for adding seo behavior to a model.
- Added Fields:
- Field 1:
- field: CharField(max_length = 255) description: Char field intended for use in html <title> tag. validation: Max Length 255 Characters default_name: seo_title
- Field 2:
- field: TextField() description: Text field intended for use in html <meta name=’description’> tag. default_name: seo_description
- Field 3:
- field: TextField() description: Text field intended for use in html <meta name=’keywords’> tag. validation: comma separated text strings default_name: seo_keywords
-
class
fusionbox.behaviors.
Timestampable
(*args, **kwargs)[source]¶ Base class for adding timestamping behavior to a model.
- Added Fields:
- Field 1:
- field: DateTimeField(default=now) description: Timestamps set at the creation of the instance default_name: created_at
- Field 2:
- field: DateTimeField(auto_now=True) description: Timestamps set each time the save method is called on the instance default_name: updated_at
-
class
fusionbox.behaviors.
Validation
(*args, **kwargs)[source]¶ Base class for adding complex validation behavior to a model.
By inheriting from Validation, your model can have
validate
andvalidate_<field>
methods.validate()
is for generic validations, and forNON_FIELD_ERRORS
, errors that do not belong to any one field. In this method you can raise a ValidationError that contains a single error message, a list of errors, or - if the messages are associated with a field - a dictionary of field-names to message-list.You can also write
validate_<field>
methods for any columns that need custom validation. This is for convience, since it is easier and more intuitive to raise an ‘invalid value’ from within one of these methods, and have it automatically associated with the correct field.Even if you don’t implement custom validation methods, Validation changes the normal behavior of
save
so that validation always occurs. This makes it easy to write APIs without having to understand theclean
,full_clean
, andclean_fields()
methods that must called in django. If a validation error occurs, the exception will not be caught, it is up to you to catch it in your view or API.
-
fusionbox.behaviors.
now
()¶ [tz] -> new datetime with tz’s local day and time.
-
class
fusionbox.behaviors.
MetaBehavior
[source] Base Metaclass for Behaviors