mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #34140 -- Reformatted code blocks in docs with blacken-docs.
This commit is contained in:
parent
6015bab80e
commit
14459f80ee
193 changed files with 5797 additions and 4481 deletions
|
@ -162,12 +162,12 @@ behave like any existing field, so we'll subclass directly from
|
|||
|
||||
from django.db import models
|
||||
|
||||
class HandField(models.Field):
|
||||
|
||||
class HandField(models.Field):
|
||||
description = "A hand of cards (bridge style)"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['max_length'] = 104
|
||||
kwargs["max_length"] = 104
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
Our ``HandField`` accepts most of the standard field options (see the list
|
||||
|
@ -259,10 +259,10 @@ we can drop it from the keyword arguments for readability::
|
|||
|
||||
from django.db import models
|
||||
|
||||
class HandField(models.Field):
|
||||
|
||||
class HandField(models.Field):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['max_length'] = 104
|
||||
kwargs["max_length"] = 104
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def deconstruct(self):
|
||||
|
@ -277,6 +277,7 @@ such as when the default value is being used::
|
|||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class CommaSepField(models.Field):
|
||||
"Implements comma-separated storage of lists"
|
||||
|
||||
|
@ -288,7 +289,7 @@ such as when the default value is being used::
|
|||
name, path, args, kwargs = super().deconstruct()
|
||||
# Only include kwarg if it's not the default
|
||||
if self.separator != ",":
|
||||
kwargs['separator'] = self.separator
|
||||
kwargs["separator"] = self.separator
|
||||
return name, path, args, kwargs
|
||||
|
||||
More complex examples are beyond the scope of this document, but remember -
|
||||
|
@ -326,7 +327,6 @@ no-op ``AlterField`` operations.
|
|||
For example::
|
||||
|
||||
class CommaSepField(models.Field):
|
||||
|
||||
@property
|
||||
def non_db_attrs(self):
|
||||
return super().non_db_attrs + ("separator",)
|
||||
|
@ -353,6 +353,7 @@ reference it::
|
|||
class CustomCharField(models.CharField):
|
||||
...
|
||||
|
||||
|
||||
class CustomTextField(models.TextField):
|
||||
...
|
||||
|
||||
|
@ -397,9 +398,10 @@ subclass ``Field`` and implement the :meth:`~Field.db_type` method, like so::
|
|||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class MytypeField(models.Field):
|
||||
def db_type(self, connection):
|
||||
return 'mytype'
|
||||
return "mytype"
|
||||
|
||||
Once you have ``MytypeField``, you can use it in any model, just like any other
|
||||
``Field`` type::
|
||||
|
@ -419,10 +421,10 @@ For example::
|
|||
|
||||
class MyDateField(models.Field):
|
||||
def db_type(self, connection):
|
||||
if connection.vendor == 'mysql':
|
||||
return 'datetime'
|
||||
if connection.vendor == "mysql":
|
||||
return "datetime"
|
||||
else:
|
||||
return 'timestamp'
|
||||
return "timestamp"
|
||||
|
||||
The :meth:`~Field.db_type` and :meth:`~Field.rel_db_type` methods are called by
|
||||
Django when the framework constructs the ``CREATE TABLE`` statements for your
|
||||
|
@ -442,7 +444,8 @@ sense to have a ``CharMaxlength25Field``, shown here::
|
|||
# This is a silly example of hard-coded parameters.
|
||||
class CharMaxlength25Field(models.Field):
|
||||
def db_type(self, connection):
|
||||
return 'char(25)'
|
||||
return "char(25)"
|
||||
|
||||
|
||||
# In the model:
|
||||
class MyModel(models.Model):
|
||||
|
@ -460,7 +463,8 @@ time -- i.e., when the class is instantiated. To do that, implement
|
|||
super().__init__(*args, **kwargs)
|
||||
|
||||
def db_type(self, connection):
|
||||
return 'char(%s)' % self.max_length
|
||||
return "char(%s)" % self.max_length
|
||||
|
||||
|
||||
# In the model:
|
||||
class MyModel(models.Model):
|
||||
|
@ -481,10 +485,10 @@ need the foreign keys that point to that field to use the same data type::
|
|||
# MySQL unsigned integer (range 0 to 4294967295).
|
||||
class UnsignedAutoField(models.AutoField):
|
||||
def db_type(self, connection):
|
||||
return 'integer UNSIGNED AUTO_INCREMENT'
|
||||
return "integer UNSIGNED AUTO_INCREMENT"
|
||||
|
||||
def rel_db_type(self, connection):
|
||||
return 'integer UNSIGNED'
|
||||
return "integer UNSIGNED"
|
||||
|
||||
.. _converting-values-to-python-objects:
|
||||
|
||||
|
@ -522,15 +526,17 @@ instances::
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
def parse_hand(hand_string):
|
||||
"""Takes a string of cards and splits into a full hand."""
|
||||
p1 = re.compile('.{26}')
|
||||
p2 = re.compile('..')
|
||||
p1 = re.compile(".{26}")
|
||||
p2 = re.compile("..")
|
||||
args = [p2.findall(x) for x in p1.findall(hand_string)]
|
||||
if len(args) != 4:
|
||||
raise ValidationError(_("Invalid input for a Hand instance"))
|
||||
return Hand(*args)
|
||||
|
||||
|
||||
class HandField(models.Field):
|
||||
# ...
|
||||
|
||||
|
@ -569,8 +575,9 @@ For example::
|
|||
# ...
|
||||
|
||||
def get_prep_value(self, value):
|
||||
return ''.join([''.join(l) for l in (value.north,
|
||||
value.east, value.south, value.west)])
|
||||
return "".join(
|
||||
["".join(l) for l in (value.north, value.east, value.south, value.west)]
|
||||
)
|
||||
|
||||
.. warning::
|
||||
|
||||
|
@ -653,7 +660,7 @@ as::
|
|||
def formfield(self, **kwargs):
|
||||
# This is a fairly standard way to set up some defaults
|
||||
# while letting the caller override them.
|
||||
defaults = {'form_class': MyFormField}
|
||||
defaults = {"form_class": MyFormField}
|
||||
defaults.update(kwargs)
|
||||
return super().formfield(**defaults)
|
||||
|
||||
|
@ -680,7 +687,7 @@ For example::
|
|||
# ...
|
||||
|
||||
def get_internal_type(self):
|
||||
return 'CharField'
|
||||
return "CharField"
|
||||
|
||||
No matter which database backend we are using, this will mean that
|
||||
:djadmin:`migrate` and other SQL commands create the right column type for
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue