I found myself copying a few Django database field classes from project to project so I decided to bundle them together
and release them as an open source package instead. Here are the fields that are currently included:
TimeZoneField
This field stores a reference to a time zone in the database and automatically instantiates the correct pytz timezone object on retrieval. Useful for recording a user’s default time zone.MarkdownCharField
This field is meant to be used in collaboration with a regular CharField. The regular CharField stores the Markdown-formatted source text and this field will store the rendered HTML version of that same text. Use thepopulate_from
argument to indicated which field on the same model contains the source text. If theallow_html
argument isFalse
(the default), any HTML tags present in the source text will be removed using thebleach library. Theextensions
argument can be used to enable any desiredextensions for theMarkdown library. One extension is enabled by default:SmartyPants. SmartyPants is configured to use thecorrect Unicode characters (e.g. “) rather than HTML entities (e.g.“
). Since CharFields don’t normallycontain paragraphs of text, MarkdownCharField strips the<p></p>
tags that Markdown always includes.MarkdownTextField
This field is meant to be used in collaboration with a regular TextField. The regular TextField stores the Markdown-formatted source text and this field will store the rendered HTML version of that same text. Use thepopulate_from
argument to indicated which field on the same model contains the source text. If theallow_html
argument isFalse
(the default), any HTML tags present in the source text will be removed using thebleach library. Theextensions
argument can be used to enable any desiredextensions for theMarkdown library. One extension is enabled by default:SmartyPants. SmartyPants is configured to use thecorrect Unicode characters (e.g. “) rather than HTML entities (e.g.“
).UUIDPrimaryKeyField
UUIDs are, in many ways, better primary keys than automatically incrementing integer fields. Unfortunately, thedefault Django implementation—UUIDField
—requires some boilerplate (including importing theuuid
module) beforeit can be used as a primary key. UUIDPrimaryKeyField manages the boilerplate for you. Just addid = UUIDPrimaryKeyField()
to your model and everything will just work.