Package: django-extra-tools
Installationpipinstalldjango-extra-toolsQuick startEnabledjango-extra-toolsINSTALLED_APPS=[...'django_extra_tools',]Install SQL functionspythonmanage.pymigrateStringAgg aggregate function on Django 1.4 needs monkey patchfromdjango_extra_tools.monkeyimportpatch_djangopatch_all()Template filtersparse_datetimeParse datetime from string.{%loadparse%}{{string_datetime|parse_datetime|date:"Y-m-d H:i"}}parse_dateParse date from string.{%loadparse%}{{string_date|parse_date|date:"Y-m-d"}}parse_timeParse time from string.{%loadparse%}{{string_time|parse_time|date:"H:i"}}parse_durationParse duration (timedelta) from string.{%loadparse%}{{string_duration|parse_duration}}AggregationFirstReturns the first non-NULL item.fromdjango_extra_tools.db.models.aggregatesimportFirstTable.objects.aggregate(First('col1',order_by='col2'))LastReturns the last non-NULL item.fromdjango_extra_tools.db.models.aggregatesimportLastTable.objects.aggregate(Last('col1',order_by='col2'))MedianReturns median value.fromdjango_extra_tools.db.models.aggregatesimportMedianTable.objects.aggregate(Median('col1'))StringAggCombines the values as the text. Fields are separated by a “separator”.fromdjango_extra_tools.db.models.aggregatesimportStringAggTable.objects.aggregate(StringAgg('col1'))Model mixinsCreatedAtMixinAddcreated_atfield to model.fromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.CreatedAtMixin,models.Model):passmodel=MyModel()print(model.created_at)CreatedByMixinAddcreated_byfield to model.fromdjango.contrib.auth.modelsimportUserfromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.CreatedByMixin,models.Model):passuser=User.objects.get(username='user')model=MyModel(created_by=user)print(model.created_by)UpdatedAtMixinAddupdated_atfield to model.fromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.UpdatedAtMixin,models.Model):operation=models.CharField(max_length=10)model=MyModel()print(model.updated_at)model.operation='update'model.save()print(model.updated_at)UpdatedByMixinAddupdated_byfield to model.fromdjango.contrib.auth.modelsimportUserfromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.UpdatedByMixin,models.Model):operation=models.CharField(max_length=10)user=User.objects.get(username='user')model=MyModel()print(model.updated_by)model.operation='update'model.save_by(user)print(model.updated_by)DeletedAtMixinAdddeleted_atfield to model.fromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.DeletedAtMixin,models.Model):passmodel=MyModel()print(model.deleted_at)model.delete()print(model.deleted_at)DeletedByMixinAdddeleted_byfield to model.fromdjango.contrib.auth.modelsimportUserfromdjango.dbimportmodelsfromdjango_extra_tools.db.modelsimporttimestampableclassMyModel(timestampable.DeletedByMixin,models.Model):passuser=User.objects.get(username='user')model=MyModel()print(model.deleted_by)model.delete_by(user)print(model.deleted_by)CreatedMixinAddcreated_atandcreated_byfields to model.UpdatedMixinAddupdated_atandupdated_byfields to model.DeletedMixinAdddeleted_atanddeleted_byfields to model.Database functionsbatch_qsReturns a (start, end, total, queryset) tuple for each batch in the given queryset.fromdjango_extra_tools.db.modelsimportbatch_qsqs=Table.objects.all()start,end,total,queryset=batch_qs(qs,10)pg_versionReturn tuple with PostgreSQL version of a specific connection.fromdjango_extra_tools.db.modelsimportpg_versionversion=pg_version()HTTP ResponseHttpResponseGetFileAn HTTP response class with the “download file” headers.fromdjango_extra_tools.httpimportHttpResponseGetFilereturnHttpResponseGetFile(filename='file.txt',content=b'file content',content_type='file/text')WSGI Requestget_client_ipGet the client IP from the request.fromdjango_extra_tools.wsgi_requestimportget_client_ipip=get_client_ip(request)ManagementOneInstanceCommandA management command which will be run only one instance of command with
namename. No other command with namenamecan not be run in the
same time.fromdjango_extra_tools.managementimportOneInstanceCommandclassCommand(OneInstanceCommand):name='mycommand'defhandle_instance(self,*args,**options):# some operationsNagiosCheckCommandA management command which perform a Nagios check.fromdjango_extra_tools.managementimportNagiosCheckCommandclassCommand(NagiosCheckCommand):defhandle_nagios_check(self,*args,**options):returnself.STATE_OK,'OK'MiddlewareXhrMiddlewareThis middleware allows cross-domain XHR using the html5 postMessage API.MIDDLEWARE_CLASSES=(...'django_extra_tools.middleware.XhrMiddleware')XHR_MIDDLEWARE_ALLOWED_ORIGINS='*'XHR_MIDDLEWARE_ALLOWED_METHODS=['POST','GET','OPTIONS','PUT','DELETE']XHR_MIDDLEWARE_ALLOWED_HEADERS=['Content-Type','Authorization','Location','*']XHR_MIDDLEWARE_ALLOWED_CREDENTIALS='true'XHR_MIDDLEWARE_EXPOSE_HEADERS=['Location']