django.core.management.base BaseCommand Example Code

BaseCommand is a Django object for creating new Django admin commands that can be invoked with the manage.py script. The Django project team as usual provides fantastic documentation for creating your own commands. There are also some well-written community tutorials on the subject such as How to Create Custom Django Management Commands by Vitor Freitas.

Example 1 from django-filer

django-filer (project documentation) is a file management library for uploading and organizing files and images in Django's admin interface. The project also installs a few Django manage.py commands to make it easier to work with the files and images that you upload. The project's code is available under the BSD 3-Clause "New" or "Revised" open source license.

django-filer / filer / management / commands / generate_thumbnails.py

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand

from filer.models.imagemodels import Image


class Command(BaseCommand):

    def handle(self, *args, **options):
        """
        Generates image thumbnails
        NOTE: To keep memory consumption stable avoid iteration 
        over the Image queryset
        """
        pks = Image.objects.all().values_list('id', flat=True)
        total = len(pks)
        for idx, pk in enumerate(pks):
            image = None
            try:
                image = Image.objects.get(pk=pk)
                self.stdout.write(u'Processing image {0} / {1} {2}'.\
                    format(idx + 1, total, image))
                self.stdout.flush()
                image.thumbnails
                image.icons
            except IOError as e:
                self.stderr.write('Failed to generate thumbnails: {0}'\
                    .format(str(e)))
                self.stderr.flush()
            finally:
                del image
1. Introduction 2. Development Environments 3. Data 4. Web Development 5. Deployment 6. DevOps Changelog What Full Stack Means About the Author Future Directions Page Statuses Django ExtensionsDjango Example Codedjango.apps.config AppConfigdjango.conf settingsdjango.conf.urls.urldjango.contrib.admindjango.contrib.admin.filters SimpleListFilterdjango.contrib.admin.sites registerdjango.contrib.auth.decorators login_requireddjango.contrib.auth get_user_modeldjango.contrib.auth.hashers make_passworddjango.core.exceptions ImproperlyConfigureddjango.core.mail.messages EmailMessagedjango.core.mail.send_maildjango.core.management.base BaseCommanddjango.db.models AutoFielddjango.db.models BooleanFielddjango.db.models CharFielddjango.db.models DateFielddjango.db.models DateTimeFielddjango.db.models FileFielddjango.db.models ForeignKeydjango.db.models GenericIPAddressFielddjango.db.models ImageFielddjango.db.models IntegerFielddjango.db.models Modeldjango.db.models PositiveIntegerFielddjango.db.models PositiveSmallIntegerFielddjango.db.models.signaldjango.db.models SlugFielddjango.db.models SmallIntegerFielddjango.db.models TextFielddjango.db OperationalErrordjango.dispatch Signaldjango.formsdjango.forms BooleanFielddjango.forms CharFielddjango.forms ChoiceFielddjango.forms EmailFielddjango.forms IntegerFielddjango.http Http404django.http HttpResponsedjango.http HttpResponseBadRequestdjango.http HttpResponseForbiddendjango.http HttpResponseNotModifieddjango.http HttpResponsePermanentRedirectdjango.http HttpResponseRedirectdjango.template.response SimpleTemplateResponsedjango.template.response TemplateResponsedjango.urls.pathdjango.urls reverse_lazydjango.urls.exceptions NoReverseMatchdjango.urls.exceptions Resolver404django.utils.html format_htmldjango.utils.timezone ...or view the full table of contents.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2020