![](http://habrastorage.org/getpro/habr/avatars/43c/a1a/e3e/43ca1ae3e6d0ad7b996d2fd50657b44b.jpg)
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.
![image](https://habrastorage.org/getpro/habr/post_images/37e/465/2cc/37e4652cc459cf199d92030a0887268e.jpg)
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:
Ability to specify the necessary manager when using model binding:
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:
Ability to define your own search types for filtering when using ORM.
django.contrib.admin
django.contrib.auth
django.contrib.sites
E-mail
Upload files
Forms
Internationalization
Management teams
Models
Requests and Answers
Utilities
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 .
![image](https://habrastorage.org/getpro/habr/post_images/37e/465/2cc/37e4652cc459cf199d92030a0887268e.jpg)
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()
Food.objects.pizzas().vegetarian()
Ability to specify the necessary manager when using model binding:
class Blog(models.Model):
pass
class Entry(models.Model):
blog = models.ForeignKey(Blog)
objects = models.Manager() # Default Manager
entries = EntryManager() # Custom Manager
b = Blog.objects.get(id=1)
b.entry_set(manager='entries').all()
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()
try:
c.execute(...)
finally:
c.close()
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
IntegerField.register_lookup(AbsoluteValue)
# Использование
Experiment.objects.filter(change__abs=27)
# В результате получим
# SELECT ... WHERE ABS("experiments"."change") = 27
Other interesting changes
django.contrib.admin
- Now you can implement your own site_header, site_title, and index_title, without having to redefine the template.
- The ModelAdmin.get_fields () method can be overridden to set ModelAdmin.fields values.
- In addition to the existing admin.site.register syntax, you can use the new register () decorator .
- You can set ModelAdmin.list_display_links = None to disable links in the grid
- To control the "View on site" button, you can define your ModelAdmin.view_on_site .
- Ability to specify sorting for ModelAdmin.list_display , substituting a hyphen in admin_order_field.
- The ModelAdmin.get_changeform_initial_data () method can be overridden to modify form data.
django.contrib.auth
- ** kwargs sent to email_user () are also sent to send_mail () .
- the permission_required () decorator can receive a list of permissions or only one permission.
- it is possible to override the new AuthenticationForm.confirm_login_allowed () method to quickly and easily configure authorization policies.
- django.contrib.auth.views.password_reset () accepts the optional html_email_template_name parameter. The parameter is used to send html letters when resetting the password.
- Added AbstractBaseUser.get_session_auth_hash () method . If the user model is inherited from AbstractBaseUser , then changing the user's password will reset his obsolete sessions.
django.contrib.sites
- The new middleware django.contrib.sites.middleware.CurrentSiteMiddleware allows you to set the current site for every request.
- In send_mail () added parameter html_message, for sending messages such as text / html.
- The SMTP backend was able to set the timeout parameter .
Upload files
- Added attribute UploadedFile.content_type_extra , which contains additional parameters passed in the header of the content type when downloading files.
- The new FILE_UPLOAD_DIRECTORY_PERMISSIONS setting allows you to set permissions on the created directories when downloading files. The same setting directly for files is performed by FILE_UPLOAD_PERMISSIONS .
- FileField.upload_to is now optional.
Forms
- 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.
Internationalization
- 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.
Models
- 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
- The new HttpRequest.scheme attribute defines the request scheme (http or https).
- redirect () supports relative URLs.
- The new subclass of HttpResponse is JsonResponse .
Utilities
- Improved strip_tags () .
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