Package: django-model-mixins
django-model-mixinsMixins for Django models.InstallationTo get the latest stable release from PyPIpip install django-model-mixinsAdddjango_model_mixinsto yourINSTALLED_APPSINSTALLED_APPS=(...,'django_model_mixins',)UsageQuickstartInherit from mixins in your models, e.g.fromdjango_model_mixinsimportmixinsclassArticle(mixins.CreationDateMixin,mixins.RandomIDMixin):# A `created_at` and `id` field is automatically generated on migrations.passEdit settings per model (not per instance!), e.g.importstringfromdjango_model_mixinsimportmixinsclassEmailVerify(mixins.TokenMixin,mixins.RandomIDMixin):TOKEN_LENGTH=255# Token is 255 chars long for every `EmailVerify`classPasswordReset(mixins.TokenMixin,mixins.RandomIDMixin):TOKEN_LENGTH=6TOKEN_CHARS=string.digits# Token is 6 digits long for every `PasswordReset`.Edit settings globally, e.g:settings.py# Don't create a token automatically on model creation.TOKEN_CREATE_ON_CREATION=FalseModel settings overwrite global settings.Available modelsRandomIDMixinAdds a uniqueidfield withprimary_key=True.
The id is cryptographically generated on model creation.SettingsSettings nameModel nameDefault valueDescriptionRANDOM_ID_MAX_LENGTH1023max_lengthvalue for theidfield. This value can only be globally set.RANDOM_ID_CHARSID_CHARSstring.ascii_letters + string.digitsCharacter pool the generator can choose from.ID_LENGTHRANDOM_ID_MAX_LENGTHThe length of the id.Examplesfromdjango_model_mixinsimportmixinsclassArticle(mixins.RandomIDMixin):passfromdjango_model_mixinsimportmixinsclassProfile(mixins.RandomIDMixin):ID_LENGTH=20CreationDateMixinAdds acreated_atfield which is automatically set to current time (usingtimezone.now())
on model creation.Examplesfromdjango_model_mixinsimportmixinsclassArticle(mixins.CreationDateMixin):passEditDateMixinAdds aedited_atfield which is automatically set to current time (usingtimezone.now())
on every model save.Examplesfromdjango_model_mixinsimportmixinsclassArticle(mixins.EditDateMixin):passTokenMixinAdds atokenfield which can be used as an alternative to aid.
Thetokencan be changed anytime, whereasidis immutable.The token is generated cryptographically.SettingsSettings nameModel nameDefault valueDescriptionTOKEN_MAX_LENGTH2047max_lengthvalue for thetokenfield. This value can only be globally set.TOKEN_CHARSTOKEN_CHARSstring.ascii_letters + string.digitsCharacter pool the generator can choose from.TOKEN_LENGTHTOKEN_MAX_LENGTHThe length of the token.TOKEN_CREATE_ON_CREATIONCREATE_ON_CREATIONTrueWhether a token should automatically be created on model creation.Examplesimportstringfromdjango_model_mixinsimportmixinsclassPasswordReset(mixins.TokenMixin):TOKEN_CHARS=string.digitsTOKEN_LENGTH=6Methodstoken.recreate_token()Recreates the token and saves it.TokenGeneratedAtMixinInherits fromTokenMixin.Adds atoken_generatedfield which represents the last date a token was created (or recreated.)Youneedto specify aTOKEN_VALID_DURATION_IN_SECONDSfield per model.SettingsSettings nameModel nameDefault valueDescriptionTOKEN_VALID_DURATION_IN_SECONDSThe duration in seconds how long a token is valid.intexpected. This field is required.Examplesimportstringfromdjango_model_mixinsimportmixinsclassPasswordReset(mixins.TokenGeneratedAtMixin):TOKEN_CHARS=string.digitsTOKEN_LENGTH=6# Token is valid for 15 minutesTOKEN_VALID_DURATION_IN_SECONDS=60*15Methodstoken.expire_dateThe date till when the token is valid.token.is_expiredReturn whether the token is expired.