What new awaits us in Django 1.7

    This post provides an overview of the innovations and features of the popular Django 1.7 framework among Python developers. The release is positioned both by the community and by the main developers - as the most significant release since the release of Django 1.0.


    Innovations in version 1.7

    Discontinuing Python 2.6. Python version 2.7 and higher is now supported. Declared support for Python 3.4.
    Added native support for migrations directly to the framework itself. You can thank the author of the popular South battery, Andrew Godwin, for this .

    The syncdb command is deprecated and will be removed in Django 1.9. Until then, syncdb is an alias for the migrate command, which provides both migrations and the old syncdb behavior.
    Init_data fixtures are no longer initialized by default for applications with migrations. It is proposed to use the fixture loading at the level of the migrations themselves. Application loading

    mechanism- was subjected to full refactoring. As a result, you can abandon models.py, which previously identified the application and was required.
    New methods for subclassing Field . The main feature is the required deconstruct () method. Unfortunately, this is the fault of including migrations in Django. If you inherit from standard fields and do not override the __init__ method, then you won’t have to worry about it.

    Now you can call QuerySets directly from the manager:

    class FoodQuerySet(models.QuerySet):
        def pizzas(self):
            return self.filter(kind='pizza')
        def vegetarian(self):
            return self.filter(vegetarian=True)
    class Food(models.Model):
        kind = models.CharField(max_length=50)
        vegetarian = models.BooleanField()
        objects = FoodQuerySet.as_manager()

    Ability to specify the necessary manager when using model binding:
    class Blog(models.Model):
    class Entry(models.Model):
        blog = models.ForeignKey(Blog)
        objects = models.Manager()  # Default Manager
        entries = EntryManager()    # Custom Manager
    b = Blog.objects.get(id=1)

    A new system, for checking the project (System check), which at startup detects problems and tells what and how to fix. For verification, a new check command is used, which replaced the obsolete validate command.
    New Prefetch for advanced prefetch_related operations. Now you can configure prefetching using QuerySets.

    Support for the current time zone in the admin panel when working with the date widget. Previously, the browser time zone was used. If there is a discrepancy between the time in the browser and on the server, a visual prompt is displayed.

    The database cursor can now be used as a context manager, which is an abbreviation for:

    c = connection.cursor()

    Ability to define your own search types for filtering when using ORM.

    from django.db.models import IntegerField
    from django.db.models import Transform
    class AbsoluteValue(Transform):
        lookup_name = 'abs'
        def as_sql(self, qn, connection):
            lhs, params = qn.compile(self.lhs)
            return "ABS(%s)" % lhs, params
    # Использование
    # В результате получим
    # SELECT ... WHERE ABS("experiments"."change") = 27

    Other interesting changes





    Upload files

    • Textarea now includes the max_length attribute if max_length is defined in the model.
    • Field.choices allows you to set value to an empty value. Default "-------".
    • In forms in the clean () method, self.cleaned_data is no longer required to be returned.
    • Now you can remove fields from the form by setting their name to None.
    • The new add_error () method allows you to set errors for specific fields.
    • Added the ability to set and output errors to delimiters of the form unique, unique_for_date, and unique_together.

    • The django.middleware.locale.LocaleMiddleware.response_redirect_class attribute allows you to configure redirects.
    • LocaleMiddleware stores the selected language by the user language in _language. You can access using the constant LANGUAGE_SESSION_KEY.
    • The blocktrans tag removes the trimmed attribute. This option removes newlines from beginning to end, combining them separated by spaces. Which is convenient for generating locale files.
    • Improved makemessages .
    • The following language constants have been added: LANGUAGE_COOKIE_AGE, LANGUAGE_COOKIE_DOMAIN and LANGUAGE_COOKIE_PATH.

    Management teams
    • Ability to disable color output to the console.
    • Added the ability to dump data with primary keys during serialization.
    • There is no need to specify the name of the table cache when using createcachetable. Now Django does it herself, taking into account the cache settings in the project settings.
    • The runserver team has been improved. Now, with pyinotify installed, the reload speed has become higher and consumes less battery on laptops. Also, the server reboots when using the compilemessages command. All static requests that were previously filtered are displayed in the console.

    • New QuerySet.update_or_create () method .
    • The new Meta option is default_permissions , which allows you to configure the create / modify / delete operations.
    • OneToOneField detection on inheritance in abstract classes.
    • Added the ability to use None as the request value when using iexact .
    • The ability to use a single list in index_together when specifying a single set of fields (not a list in lists).
    • Numeric fields are now checked depending on the database. Previously, it could lead to an error.

    Requests and Answers


    This post is a free interpretation of the official documentation under development, and is for informational purposes only.
    It includes the most interesting things from the point of view of the author of the post. For a more detailed and detailed acquaintance, you can
    go to the documentation page .

    Only registered users can participate in the survey. Please come in.

    Are you expecting a release?

    • 69.4% Yes 504
    • 6.3% No 46
    • 23.6% I don't care 172
    • 7.7% already use 56

    Also popular now: