doc_content
stringlengths
1
386k
doc_id
stringlengths
5
188
bulk_create(objs, batch_size=None, ignore_conflicts=False)
django.ref.models.querysets#django.db.models.query.QuerySet.bulk_create
bulk_update(objs, fields, batch_size=None)
django.ref.models.querysets#django.db.models.query.QuerySet.bulk_update
contains(obj)
django.ref.models.querysets#django.db.models.query.QuerySet.contains
count()
django.ref.models.querysets#django.db.models.query.QuerySet.count
create(**kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.create
dates(field, kind, order='ASC')
django.ref.models.querysets#django.db.models.query.QuerySet.dates
datetimes(field_name, kind, order='ASC', tzinfo=None, is_dst=None)
django.ref.models.querysets#django.db.models.query.QuerySet.datetimes
db The database that will be used if this query is executed now.
django.ref.models.querysets#django.db.models.query.QuerySet.db
defer(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.defer
delete()
django.ref.models.querysets#django.db.models.query.QuerySet.delete
difference(*other_qs)
django.ref.models.querysets#django.db.models.query.QuerySet.difference
distinct(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.distinct
earliest(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.earliest
exclude(*args, **kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.exclude
exists()
django.ref.models.querysets#django.db.models.query.QuerySet.exists
explain(format=None, **options)
django.ref.models.querysets#django.db.models.query.QuerySet.explain
extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
django.ref.models.querysets#django.db.models.query.QuerySet.extra
filter(*args, **kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.filter
first()
django.ref.models.querysets#django.db.models.query.QuerySet.first
get(*args, **kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.get
get_or_create(defaults=None, **kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.get_or_create
in_bulk(id_list=None, *, field_name='pk')
django.ref.models.querysets#django.db.models.query.QuerySet.in_bulk
intersection(*other_qs)
django.ref.models.querysets#django.db.models.query.QuerySet.intersection
iterator(chunk_size=2000)
django.ref.models.querysets#django.db.models.query.QuerySet.iterator
last()
django.ref.models.querysets#django.db.models.query.QuerySet.last
latest(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.latest
none()
django.ref.models.querysets#django.db.models.query.QuerySet.none
only(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.only
order_by(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.order_by
ordered True if the QuerySet is ordered — i.e. has an order_by() clause or a default ordering on the model. False otherwise.
django.ref.models.querysets#django.db.models.query.QuerySet.ordered
prefetch_related(*lookups)
django.ref.models.querysets#django.db.models.query.QuerySet.prefetch_related
raw(raw_query, params=(), translations=None, using=None)
django.ref.models.querysets#django.db.models.query.QuerySet.raw
reverse()
django.ref.models.querysets#django.db.models.query.QuerySet.reverse
select_for_update(nowait=False, skip_locked=False, of=(), no_key=False)
django.ref.models.querysets#django.db.models.query.QuerySet.select_for_update
select_related(*fields)
django.ref.models.querysets#django.db.models.query.QuerySet.select_related
union(*other_qs, all=False)
django.ref.models.querysets#django.db.models.query.QuerySet.union
update(**kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.update
update_or_create(defaults=None, **kwargs)
django.ref.models.querysets#django.db.models.query.QuerySet.update_or_create
using(alias)
django.ref.models.querysets#django.db.models.query.QuerySet.using
values(*fields, **expressions)
django.ref.models.querysets#django.db.models.query.QuerySet.values
values_list(*fields, flat=False, named=False)
django.ref.models.querysets#django.db.models.query.QuerySet.values_list
RESTRICT Prevent deletion of the referenced object by raising RestrictedError (a subclass of django.db.IntegrityError). Unlike PROTECT, deletion of the referenced object is allowed if it also references a different object that is being deleted in the same operation, but via a CASCADE relationship. Consider this set of models: class Artist(models.Model): name = models.CharField(max_length=10) class Album(models.Model): artist = models.ForeignKey(Artist, on_delete=models.CASCADE) class Song(models.Model): artist = models.ForeignKey(Artist, on_delete=models.CASCADE) album = models.ForeignKey(Album, on_delete=models.RESTRICT) Artist can be deleted even if that implies deleting an Album which is referenced by a Song, because Song also references Artist itself through a cascading relationship. For example: >>> artist_one = Artist.objects.create(name='artist one') >>> artist_two = Artist.objects.create(name='artist two') >>> album_one = Album.objects.create(artist=artist_one) >>> album_two = Album.objects.create(artist=artist_two) >>> song_one = Song.objects.create(artist=artist_one, album=album_one) >>> song_two = Song.objects.create(artist=artist_one, album=album_two) >>> album_one.delete() # Raises RestrictedError. >>> artist_two.delete() # Raises RestrictedError. >>> artist_one.delete() (4, {'Song': 2, 'Album': 1, 'Artist': 1})
django.ref.models.fields#django.db.models.RESTRICT
SET() Set the ForeignKey to the value passed to SET(), or if a callable is passed in, the result of calling it. In most cases, passing a callable will be necessary to avoid executing queries at the time your models.py is imported: from django.conf import settings from django.contrib.auth import get_user_model from django.db import models def get_sentinel_user(): return get_user_model().objects.get_or_create(username='deleted')[0] class MyModel(models.Model): user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user), )
django.ref.models.fields#django.db.models.SET
SET_DEFAULT Set the ForeignKey to its default value; a default for the ForeignKey must be set.
django.ref.models.fields#django.db.models.SET_DEFAULT
SET_NULL Set the ForeignKey null; this is only possible if null is True.
django.ref.models.fields#django.db.models.SET_NULL
django.db.models.signals.class_prepared
django.ref.signals#django.db.models.signals.class_prepared
django.db.models.signals.m2m_changed
django.ref.signals#django.db.models.signals.m2m_changed
django.db.models.signals.post_delete
django.ref.signals#django.db.models.signals.post_delete
django.db.models.signals.post_init
django.ref.signals#django.db.models.signals.post_init
django.db.models.signals.post_migrate
django.ref.signals#django.db.models.signals.post_migrate
django.db.models.signals.post_save
django.ref.signals#django.db.models.signals.post_save
django.db.models.signals.pre_delete
django.ref.signals#django.db.models.signals.pre_delete
django.db.models.signals.pre_init
django.ref.signals#django.db.models.signals.pre_init
django.db.models.signals.pre_migrate
django.ref.signals#django.db.models.signals.pre_migrate
django.db.models.signals.pre_save
django.ref.signals#django.db.models.signals.pre_save
class SlugField(max_length=50, **options)
django.ref.models.fields#django.db.models.SlugField
SlugField.allow_unicode If True, the field accepts Unicode letters in addition to ASCII letters. Defaults to False.
django.ref.models.fields#django.db.models.SlugField.allow_unicode
class SmallAutoField(**options)
django.ref.models.fields#django.db.models.SmallAutoField
class SmallIntegerField(**options)
django.ref.models.fields#django.db.models.SmallIntegerField
class StdDev(expression, output_field=None, sample=False, filter=None, default=None, **extra) Returns the standard deviation of the data in the provided expression. Default alias: <field>__stddev Return type: float if input is int, otherwise same as input field, or output_field if supplied Has one optional argument: sample By default, StdDev returns the population standard deviation. However, if sample=True, the return value will be the sample standard deviation.
django.ref.models.querysets#django.db.models.StdDev
sample By default, StdDev returns the population standard deviation. However, if sample=True, the return value will be the sample standard deviation.
django.ref.models.querysets#django.db.models.StdDev.sample
class Subquery(queryset, output_field=None)
django.ref.models.expressions#django.db.models.Subquery
class Sum(expression, output_field=None, distinct=False, filter=None, default=None, **extra) Computes the sum of all values of the given expression. Default alias: <field>__sum Return type: same as input field, or output_field if supplied Has one optional argument: distinct If distinct=True, Sum returns the sum of unique values. This is the SQL equivalent of SUM(DISTINCT <field>). The default value is False.
django.ref.models.querysets#django.db.models.Sum
distinct If distinct=True, Sum returns the sum of unique values. This is the SQL equivalent of SUM(DISTINCT <field>). The default value is False.
django.ref.models.querysets#django.db.models.Sum.distinct
class TextField(**options)
django.ref.models.fields#django.db.models.TextField
TextField.db_collation New in Django 3.2. The database collation name of the field. Note Collation names are not standardized. As such, this will not be portable across multiple database backends. Oracle Oracle does not support collations for a TextField.
django.ref.models.fields#django.db.models.TextField.db_collation
class TimeField(auto_now=False, auto_now_add=False, **options)
django.ref.models.fields#django.db.models.TimeField
class Transform A Transform is a generic class to implement field transformations. A prominent example is __year that transforms a DateField into a IntegerField. The notation to use a Transform in a lookup expression is <expression>__<transformation> (e.g. date__year). This class follows the Query Expression API, which implies that you can use <expression>__<transform1>__<transform2>. It’s a specialized Func() expression that only accepts one argument. It can also be used on the right hand side of a filter or directly as an annotation. bilateral A boolean indicating whether this transformation should apply to both lhs and rhs. Bilateral transformations will be applied to rhs in the same order as they appear in the lookup expression. By default it is set to False. For example usage, see How to write custom lookups. lhs The left-hand side - what is being transformed. It must follow the Query Expression API. lookup_name The name of the lookup, used for identifying it on parsing query expressions. It cannot contain the string "__". output_field Defines the class this transformation outputs. It must be a Field instance. By default is the same as its lhs.output_field.
django.ref.models.lookups#django.db.models.Transform
bilateral A boolean indicating whether this transformation should apply to both lhs and rhs. Bilateral transformations will be applied to rhs in the same order as they appear in the lookup expression. By default it is set to False. For example usage, see How to write custom lookups.
django.ref.models.lookups#django.db.models.Transform.bilateral
lhs The left-hand side - what is being transformed. It must follow the Query Expression API.
django.ref.models.lookups#django.db.models.Transform.lhs
lookup_name The name of the lookup, used for identifying it on parsing query expressions. It cannot contain the string "__".
django.ref.models.lookups#django.db.models.Transform.lookup_name
output_field Defines the class this transformation outputs. It must be a Field instance. By default is the same as its lhs.output_field.
django.ref.models.lookups#django.db.models.Transform.output_field
class UniqueConstraint(*expressions, fields=(), name=None, condition=None, deferrable=None, include=None, opclasses=()) Creates a unique constraint in the database.
django.ref.models.constraints#django.db.models.UniqueConstraint
UniqueConstraint.condition
django.ref.models.constraints#django.db.models.UniqueConstraint.condition
UniqueConstraint.deferrable
django.ref.models.constraints#django.db.models.UniqueConstraint.deferrable
UniqueConstraint.expressions
django.ref.models.constraints#django.db.models.UniqueConstraint.expressions
UniqueConstraint.fields
django.ref.models.constraints#django.db.models.UniqueConstraint.fields
UniqueConstraint.include
django.ref.models.constraints#django.db.models.UniqueConstraint.include
UniqueConstraint.name
django.ref.models.constraints#django.db.models.UniqueConstraint.name
UniqueConstraint.opclasses
django.ref.models.constraints#django.db.models.UniqueConstraint.opclasses
class URLField(max_length=200, **options)
django.ref.models.fields#django.db.models.URLField
class UUIDField(**options)
django.ref.models.fields#django.db.models.UUIDField
class Value(value, output_field=None)
django.ref.models.expressions#django.db.models.Value
class Variance(expression, output_field=None, sample=False, filter=None, default=None, **extra) Returns the variance of the data in the provided expression. Default alias: <field>__variance Return type: float if input is int, otherwise same as input field, or output_field if supplied Has one optional argument: sample By default, Variance returns the population variance. However, if sample=True, the return value will be the sample variance.
django.ref.models.querysets#django.db.models.Variance
sample By default, Variance returns the population variance. However, if sample=True, the return value will be the sample variance.
django.ref.models.querysets#django.db.models.Variance.sample
atomic(using=None, savepoint=True, durable=False) Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back. atomic blocks can be nested. In this case, when an inner block completes successfully, its effects can still be rolled back if an exception is raised in the outer block at a later point. It is sometimes useful to ensure an atomic block is always the outermost atomic block, ensuring that any database changes are committed when the block is exited without errors. This is known as durability and can be achieved by setting durable=True. If the atomic block is nested within another it raises a RuntimeError. atomic is usable both as a decorator: from django.db import transaction @transaction.atomic def viewfunc(request): # This code executes inside a transaction. do_stuff() and as a context manager: from django.db import transaction def viewfunc(request): # This code executes in autocommit mode (Django's default). do_stuff() with transaction.atomic(): # This code executes inside a transaction. do_more_stuff() Wrapping atomic in a try/except block allows for natural handling of integrity errors: from django.db import IntegrityError, transaction @transaction.atomic def viewfunc(request): create_parent() try: with transaction.atomic(): generate_relationships() except IntegrityError: handle_exception() add_children() In this example, even if generate_relationships() causes a database error by breaking an integrity constraint, you can execute queries in add_children(), and the changes from create_parent() are still there and bound to the same transaction. Note that any operations attempted in generate_relationships() will already have been rolled back safely when handle_exception() is called, so the exception handler can also operate on the database if necessary. Avoid catching exceptions inside atomic! When exiting an atomic block, Django looks at whether it’s exited normally or with an exception to determine whether to commit or roll back. If you catch and handle exceptions inside an atomic block, you may hide from Django the fact that a problem has happened. This can result in unexpected behavior. This is mostly a concern for DatabaseError and its subclasses such as IntegrityError. After such an error, the transaction is broken and Django will perform a rollback at the end of the atomic block. If you attempt to run database queries before the rollback happens, Django will raise a TransactionManagementError. You may also encounter this behavior when an ORM-related signal handler raises an exception. The correct way to catch database errors is around an atomic block as shown above. If necessary, add an extra atomic block for this purpose. This pattern has another advantage: it delimits explicitly which operations will be rolled back if an exception occurs. If you catch exceptions raised by raw SQL queries, Django’s behavior is unspecified and database-dependent. You may need to manually revert model state when rolling back a transaction. The values of a model’s fields won’t be reverted when a transaction rollback happens. This could lead to an inconsistent model state unless you manually restore the original field values. For example, given MyModel with an active field, this snippet ensures that the if obj.active check at the end uses the correct value if updating active to True fails in the transaction: from django.db import DatabaseError, transaction obj = MyModel(active=False) obj.active = True try: with transaction.atomic(): obj.save() except DatabaseError: obj.active = False if obj.active: ... In order to guarantee atomicity, atomic disables some APIs. Attempting to commit, roll back, or change the autocommit state of the database connection within an atomic block will raise an exception. atomic takes a using argument which should be the name of a database. If this argument isn’t provided, Django uses the "default" database. Under the hood, Django’s transaction management code: opens a transaction when entering the outermost atomic block; creates a savepoint when entering an inner atomic block; releases or rolls back to the savepoint when exiting an inner block; commits or rolls back the transaction when exiting the outermost block. You can disable the creation of savepoints for inner blocks by setting the savepoint argument to False. If an exception occurs, Django will perform the rollback when exiting the first parent block with a savepoint if there is one, and the outermost block otherwise. Atomicity is still guaranteed by the outer transaction. This option should only be used if the overhead of savepoints is noticeable. It has the drawback of breaking the error handling described above. You may use atomic when autocommit is turned off. It will only use savepoints, even for the outermost block.
django.topics.db.transactions#django.db.transaction.atomic
clean_savepoints(using=None) Resets the counter used to generate unique savepoint IDs.
django.topics.db.transactions#django.db.transaction.clean_savepoints
commit(using=None)
django.topics.db.transactions#django.db.transaction.commit
get_autocommit(using=None)
django.topics.db.transactions#django.db.transaction.get_autocommit
get_rollback(using=None)
django.topics.db.transactions#django.db.transaction.get_rollback
non_atomic_requests(using=None) This decorator will negate the effect of ATOMIC_REQUESTS for a given view: from django.db import transaction @transaction.non_atomic_requests def my_view(request): do_stuff() @transaction.non_atomic_requests(using='other') def my_other_view(request): do_stuff_on_the_other_database() It only works if it’s applied to the view itself.
django.topics.db.transactions#django.db.transaction.non_atomic_requests
on_commit(func, using=None)
django.topics.db.transactions#django.db.transaction.on_commit
rollback(using=None)
django.topics.db.transactions#django.db.transaction.rollback
savepoint(using=None) Creates a new savepoint. This marks a point in the transaction that is known to be in a “good” state. Returns the savepoint ID (sid).
django.topics.db.transactions#django.db.transaction.savepoint
savepoint_commit(sid, using=None) Releases savepoint sid. The changes performed since the savepoint was created become part of the transaction.
django.topics.db.transactions#django.db.transaction.savepoint_commit
savepoint_rollback(sid, using=None) Rolls back the transaction to savepoint sid.
django.topics.db.transactions#django.db.transaction.savepoint_rollback
set_autocommit(autocommit, using=None)
django.topics.db.transactions#django.db.transaction.set_autocommit
set_rollback(rollback, using=None)
django.topics.db.transactions#django.db.transaction.set_rollback
receiver(signal) [source] Parameters: signal – A signal or a list of signals to connect a function to.
django.topics.signals#django.dispatch.receiver
class Signal [source]
django.topics.signals#django.dispatch.Signal