the values to use for blank in selectfields will be appended to the start of most choices lists a guide to field parameters name the name of the field specified in the model attname the attribute to use on the model object this is the same as name except in the case of foreignkeys where id is appended dbcolumn the dbcolumn specified in the model or none column the database column for this field this is the same as attname except if dbcolumn is specified code that introspects values or does other dynamic things should use attname for example this gets the primary key value of object obj getattrobj opts pk attname base class for all field types designates whether empty strings fundamentally are allowed at the database level emptystringsallowed true emptyvalues listvalidators emptyvalues these track each time a field instance is created used to retain order the autocreationcounter is used for fields that django implicitly creates creationcounter is used for all userspecified fields creationcounter 0 autocreationcounter 1 defaultvalidators default set of validators defaulterrormessages invalidchoice value valuer is not a valid choice null this field cannot be null blank this field cannot be blank unique modelnames with this fieldlabels already exists uniquefordate translators the lookuptype is one of date year or month eg title must be unique for pubdate year fieldlabels must be unique for datefieldlabels lookuptypes systemcheckdeprecateddetails none systemcheckremoveddetails none attributes that don t affect a column definition these attributes are ignored when altering the field nondbattrs blank choices dbcolumn editable errormessages helptext limitchoicesto databaselevel options are not supported see 21961 ondelete relatedname relatedqueryname validators verbosename field flags hidden false manytomany none manytoone none onetomany none onetoone none relatedmodel none generated false descriptorclass deferredattribute generic field type description usually overridden by subclasses def descriptionself return field of type fieldtypes fieldtype self class name description propertydescription def init self verbosenamenone namenone primarykeyfalse maxlengthnone uniquefalse blankfalse nullfalse dbindexfalse relnone defaultnotprovided editabletrue serializetrue uniquefordatenone uniqueformonthnone uniqueforyearnone choicesnone helptext dbcolumnnone dbtablespacenone autocreatedfalse validators errormessagesnone dbcommentnone dbdefaultnotprovided self name name self verbosename verbosename may be set by setattributesfromname self verbosename verbosename store original for deconstruction self primarykey primarykey self maxlength self unique maxlength unique self blank self null blank null self remotefield rel self isrelation self remotefield is not none self default default if dbdefault is not notprovided and not hasattr dbdefault resolveexpression from django db models expressions import value dbdefault valuedbdefault self dbdefault dbdefault self editable editable self serialize serialize self uniquefordate uniquefordate self uniqueformonth uniqueformonth self uniqueforyear uniqueforyear self choices choices self helptext helptext self dbindex dbindex self dbcolumn dbcolumn self dbcomment dbcomment self dbtablespace dbtablespace self autocreated autocreated adjust the appropriate creation counter and save our local copy if autocreated self creationcounter field autocreationcounter field autocreationcounter 1 else self creationcounter field creationcounter field creationcounter 1 self validators listvalidators store for deconstruction later self errormessages errormessages store for deconstruction later def strself if not hasattrself model return super str model self model return s s model meta label self name def reprself check if field name is valid i e 1 does not end with an underscore 2 does not contain and 3 is not pk expect groupname value display containing nonpairs no groups choices in the form value display special case choices ab we cannot reliably check this for backends like oracle which consider null and to be equal and thus set up characterbased fields a little differently custom format for select clauses for example gis columns need to be selected as astexttable col on mysql as the table col data can t be used by django return enough information to recreate the field as a 4tuple the name of the field on the model if contributetoclass has been run the import path of the field including the class e g django db models integerfield this should be the most portable version so less specific may be better a list of positional arguments a dict of keyword arguments note that the positional or keyword arguments must contain values of the following types including inner values of collection types none bool str int float complex set frozenset list tuple dict uuid datetime datetime naive datetime date toplevel classes toplevel functions will be referenced by their full import path storage instances these have their own deconstruct method this is because the values here must be serialized into a text format possibly new python code possibly json and these are the only types with encoding handlers defined there s no need to return the exact way the field was instantiated this time just ensure that the resulting field is the same prefer keyword arguments over positional ones and omit parameters with their default values shortform way of fetching all the default parameters do correct kind of comparison work out path we shorten it for known django core fields return basic info other fields should override this uses deconstruct to clone a new copy of this field will not preserve any class attachmentsattribute names needed for totalordering this is needed because bisect does not take a comparison function order by creationcounter first for backward compatibility creationcounter s are equal compare only models we don t have to deepcopy very much here since most things are not intended to be altered after initial creation we need to avoid hitting reduce so define this slightly weird copy construct pickling should return the model meta fields instance of the field not a new copy of that field so use the app registry to load the model and then the field back fields are sometimes used without attaching them to models for example in aggregation in this case give back a plain field instance the code below will create a new empty instance of class self class then update its dict with self dict values so this is very close to normal pickle the getdefault cachedproperty can t be pickled due to lambda usage hook to generate new pk values on save this method is called when saving instances with no primary key value set if this method returns something else than none then the returned value is used when saving the new instance convert the input value into the expected python data type raising django core exceptions validationerror if the data can t be converted return the converted value subclasses should override this some validators can t be created at field initialization time this method provides a way to delay their creation until required validate value and raise validationerror if necessary subclasses should override this to provide validation logic skip validation for noneditable fields this is an optgroup so look inside the group for options convert the value s type and run validation validation errors from topython and validate are propagated return the correct value if no error is raised return the database column check constraint for this field for the provided connection works the same way as dbtype for the case that getinternaltype does not map to a preexisting model field return the database column data type for this field for the provided connection the default implementation of this method looks at the backendspecific datatypes dictionary looking up the field by its internal type a field class can implement the getinternaltype method to specify which preexisting django field class it s most similar to i e a custom field might be represented by a text column type which is the same as the textfield django field type which means the custom field s getinternaltype returns textfield but the limitation of the getinternaltype datatypes approach is that it cannot handle database column types that aren t already mapped to one of the builtin django field types in this case you can implement dbtype instead of getinternaltype to specify exactly which wacky database column type you want to use columntype is either a singleparameter function or a string return the data type that a related field pointing to this field should use for example this method is called by foreignkey and onetoonefield to determine its data type return the data type to use in the cast function dbtype connection ops castdatatypes getself getinternaltype if dbtype return dbtype self dbtypeparametersconnection return self dbtypeconnection def dbparametersself connection typestring self dbtypeconnection checkstring self dbcheckconnection return type typestring check checkstring def dbtypesuffixself connection return connection datatypessuffix getself getinternaltype def getdbconvertersself connection if hasattrself fromdbvalue return self fromdbvalue return property def uniqueself return self unique or self primarykey property def dbtablespaceself return self dbtablespace or settings defaultindextablespace property def dbreturningself register the field with the model class it belongs to if privateonly is true create a separate instance of this field for every subclass of cls even if cls is not an abstract model don t override a getfoodisplay method defined explicitly on this class but don t check methods derived from inheritance to allow overriding inherited choices for more complex inheritance structures users should override contributetoclass return a dict that when passed as kwargs to self model filter would yield all instances having the same value for this field as obj has return field s value just before saving value getattrmodelinstance self attname if not connection features supportsdefaultkeywordininsert from django db models expressions import databasedefault if isinstancevalue databasedefault return self dbdefault return value def getprepvalueself value return field s value prepared for interacting with the database backend used by the default implementations of getdbprepsave return field s value prepared for saving into a database if hasattrvalue assql return value return self getdbprepvaluevalue connectionconnection preparedfalse def hasdefaultself return the default value for this field return self getdefault cachedproperty def getdefaultself if self hasdefault if callableself default return self default return lambda self default if self dbdefault is not notprovided from django db models expressions import databasedefault return databasedefault if not self emptystringsallowed or self null and not connection features interpretsemptystringsasnulls return returnnone return str return empty string def getchoices self includeblanktrue blankchoiceblankchoicedash limitchoicestonone ordering if self choices is not none if includeblank return blankchoiceiteratorself choices blankchoice return self choices relmodel self remotefield model limitchoicesto limitchoicesto or self getlimitchoicesto choicefunc operator attrgetter self remotefield getrelatedfield attname if hasattrself remotefield getrelatedfield else pk qs relmodel defaultmanager complexfilterlimitchoicesto if ordering qs qs orderbyordering return blankchoice if includeblank else choicefuncx strx for x in qs def valuetostringself obj return strself valuefromobjectobj property def flatchoicesself return a django forms field instance for this field defaults required not self blank label capfirstself verbosename helptext self helptext if self hasdefault if callableself default defaultsinitial self default defaultsshowhiddeninitial true else defaultsinitial self getdefault if self choices is not none fields with choices get special treatment includeblank self blank or not self hasdefault or initial in kwargs defaultschoices self getchoicesincludeblankincludeblank defaultscoerce self topython if self null defaultsemptyvalue none if choicesformclass is not none formclass choicesformclass else formclass forms typedchoicefield many of the subclassspecific formfield arguments minvalue maxvalue don t apply for choice fields so be sure to only pass the values that typedchoicefield will understand for k in listkwargs if k not in coerce emptyvalue choices required widget label initial helptext errormessages showhiddeninitial disabled del kwargsk defaults updatekwargs if formclass is none formclass forms charfield return formclassdefaults def valuefromobjectself obj 10 are equal to truefalse bool converts former to latter in html checkboxes required means must be checked which is different from the choices case must select some value requiredfalse allows unchecked checkboxes passing maxlength to forms charfield means that the value s length will be validated twice this is considered acceptable since we want the value in the form field to pass into widget for example todo handle multiple backends with different feature flags autonow autonowadd and default are mutually exclusive options the use of more than one of these options together will trigger an error concrete subclasses use this in their implementations of checkfixdefaultvalue check if the given value appears to have been provided as a fixed time value and include a warning in the returned list if it does the value argument must be a date object or awarenaive datetime object if now is provided it must be a naive datetime object warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup no explicit date datetime value no checks necessary at this point value is a date object convert aware datetimes to the default time zone before casting them to dates 17742 casts dates into the format expected by the backend init is inherited from datefield warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup no explicit date datetime value no checks necessary for backwards compatibility interpret naive datetimes in local time this won t work during dst change but we can t do much about it so we let the exceptions percolate up the call stack contributetoclass is inherited from datefield it registers getnextbyfoo and getprevbyfoo for backwards compatibility interpret naive datetimes in local time this won t work during dst change but we can t do much about it so we let the exceptions percolate up the call stack casts datetimes into the format expected by the backend store timedelta objects use interval on postgresql interval day to second on oracle and bigint of microseconds on other databases maxlength254 to be compliant with rfcs 3696 and 5321 we do not exclude maxlength if it matches default as we want to change the default in future as with charfield this will cause email validation to be performed twice these validators can t be added at field initialization time since they re based on values retrieved from connection return the data type that a related field pointing to this field should use in most cases a foreign key pointing to a positive integer primary key will have an integer column data type but some databases e g mysql have an unsigned integer type in that case relatedfieldsmatchtypetrue the primary key should return its dbtype passing maxlength to forms charfield means that the value s length will be validated twice this is considered acceptable since we want the value in the form field to pass into widget for example warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup this will not use the right date in the race condition where now is just before the date change and value is just past 0 00 no explicit time datetime value no checks necessary at this point value is a datetime object not usually a good idea to pass in a datetime here it loses information but this can be a sideeffect of interacting with a database backend e g oracle so we ll be accommodating casts times into the format expected by the backend as with charfield this will cause url validation to be performed twice binary data is serialized as base64 return b64encodeself valuefromobjectobj decodeascii def topythonself value if it s a string it should be base64encoded data if isinstancevalue str return memoryviewb64decodevalue encodeascii return value class uuidfieldfield defaulterrormessages invalid values is not a valid uuid description universally unique identifier emptystringsallowed false def initself verbosenamenone kwargs kwargsmaxlength 32 super initverbosename kwargs def deconstructself name path args kwargs super deconstruct del kwargsmaxlength return name path args kwargs def getinternaltypeself return uuidfield def getprepvalueself value value super getprepvaluevalue return self topythonvalue def getdbprepvalueself value connection preparedfalse if value is none return none if not isinstancevalue uuid uuid value self topythonvalue if connection features hasnativeuuidfield return value return value hex def topythonself value if value is not none and not isinstancevalue uuid uuid inputform int if isinstancevalue int else hex try return uuid uuidinputform value except attributeerror valueerror raise exceptions validationerror self errormessagesinvalid codeinvalid paramsvalue value return value def formfieldself kwargs return super formfield formclass forms uuidfield kwargs class autofieldmixin dbreturning true def initself args kwargs kwargsblank true super initargs kwargs def checkself kwargs return super checkkwargs self checkprimarykey def checkprimarykeyself if not self primarykey return checks error autofields must set primarykeytrue objself idfields e100 else return def deconstructself name path args kwargs super deconstruct del kwargsblank kwargsprimarykey true return name path args kwargs def validateself value modelinstance pass def getdbprepvalueself value connection preparedfalse if not prepared value self getprepvaluevalue value connection ops validateautopkvaluevalue return value def contributetoclassself cls name kwargs if cls meta autofield raise valueerror model s can t have more than one autogenerated field cls meta label super contributetoclasscls name kwargs cls meta autofield self def formfieldself kwargs return none class autofieldmetatype property def subclassesself return bigautofield smallautofield def instancecheckself instance return isinstanceinstance self subclasses or super instancecheck instance def subclasscheckself subclass return issubclasssubclass self subclasses or super subclasscheck subclass class autofieldautofieldmixin integerfield metaclassautofieldmeta def getinternaltypeself return autofield def reldbtypeself connection return integerfield dbtypeconnectionconnection class bigautofieldautofieldmixin bigintegerfield def getinternaltypeself return bigautofield def reldbtypeself connection return bigintegerfield dbtypeconnectionconnection class smallautofieldautofieldmixin smallintegerfield def getinternaltypeself return smallautofield def reldbtypeself connection return smallintegerfield dbtypeconnectionconnection the values to use for blank in selectfields will be appended to the start of most choices lists a guide to field parameters name the name of the field specified in the model attname the attribute to use on the model object this is the same as name except in the case of foreignkeys where _id is appended db_column the db_column specified in the model or none column the database column for this field this is the same as attname except if db_column is specified code that introspects values or does other dynamic things should use attname for example this gets the primary key value of object obj getattr obj opts pk attname base class for all field types designates whether empty strings fundamentally are allowed at the database level these track each time a field instance is created used to retain order the auto_creation_counter is used for fields that django implicitly creates creation_counter is used for all user specified fields default set of validators translators the lookup_type is one of date year or month eg title must be unique for pub_date year attributes that don t affect a column definition these attributes are ignored when altering the field database level options are not supported see 21961 field flags generic field type description usually overridden by subclasses may be set by set_attributes_from_name store original for deconstruction adjust the appropriate creation counter and save our local copy store for deconstruction later store for deconstruction later return app_label model_label field_name for fields attached to models display the module class and name of the field check if field name is valid i e 1 does not end with an underscore 2 does not contain __ and 3 is not pk expect group_name value display containing non pairs no groups choices in the form value display special case choices ab we cannot reliably check this for backends like oracle which consider null and to be equal and thus set up character based fields a little differently custom format for select clauses for example gis columns need to be selected as astext table col on mysql as the table col data can t be used by django return enough information to recreate the field as a 4 tuple the name of the field on the model if contribute_to_class has been run the import path of the field including the class e g django db models integerfield this should be the most portable version so less specific may be better a list of positional arguments a dict of keyword arguments note that the positional or keyword arguments must contain values of the following types including inner values of collection types none bool str int float complex set frozenset list tuple dict uuid datetime datetime naive datetime date top level classes top level functions will be referenced by their full import path storage instances these have their own deconstruct method this is because the values here must be serialized into a text format possibly new python code possibly json and these are the only types with encoding handlers defined there s no need to return the exact way the field was instantiated this time just ensure that the resulting field is the same prefer keyword arguments over positional ones and omit parameters with their default values short form way of fetching all the default parameters do correct kind of comparison work out path we shorten it for known django core fields return basic info other fields should override this uses deconstruct to clone a new copy of this field will not preserve any class attachments attribute names needed for total_ordering this is needed because bisect does not take a comparison function order by creation_counter first for backward compatibility order no model fields first creation_counter s are equal compare only models we don t have to deepcopy very much here since most things are not intended to be altered after initial creation we need to avoid hitting __reduce__ so define this slightly weird copy construct pickling should return the model _meta fields instance of the field not a new copy of that field so use the app registry to load the model and then the field back fields are sometimes used without attaching them to models for example in aggregation in this case give back a plain field instance the code below will create a new empty instance of class self __class__ then update its dict with self __dict__ values so this is very close to normal pickle the _get_default cached_property can t be pickled due to lambda usage hook to generate new pk values on save this method is called when saving instances with no primary key value set if this method returns something else than none then the returned value is used when saving the new instance convert the input value into the expected python data type raising django core exceptions validationerror if the data can t be converted return the converted value subclasses should override this some validators can t be created at field initialization time this method provides a way to delay their creation until required validate value and raise validationerror if necessary subclasses should override this to provide validation logic skip validation for non editable fields this is an optgroup so look inside the group for options convert the value s type and run validation validation errors from to_python and validate are propagated return the correct value if no error is raised return the database column check constraint for this field for the provided connection works the same way as db_type for the case that get_internal_type does not map to a preexisting model field return the database column data type for this field for the provided connection the default implementation of this method looks at the backend specific data_types dictionary looking up the field by its internal type a field class can implement the get_internal_type method to specify which preexisting django field class it s most similar to i e a custom field might be represented by a text column type which is the same as the textfield django field type which means the custom field s get_internal_type returns textfield but the limitation of the get_internal_type data_types approach is that it cannot handle database column types that aren t already mapped to one of the built in django field types in this case you can implement db_type instead of get_internal_type to specify exactly which wacky database column type you want to use column_type is either a single parameter function or a string return the data type that a related field pointing to this field should use for example this method is called by foreignkey and onetoonefield to determine its data type return the data type to use in the cast function extension of db_type providing a range of different return values type checks this will look at db_type allowing custom model fields to override it private api intended only to be used by django itself register the field with the model class it belongs to if private_only is true create a separate instance of this field for every subclass of cls even if cls is not an abstract model don t override a get_foo_display method defined explicitly on this class but don t check methods derived from inheritance to allow overriding inherited choices for more complex inheritance structures users should override contribute_to_class return a dict that when passed as kwargs to self model filter would yield all instances having the same value for this field as obj has return field s value just before saving perform preliminary non db specific value checks and conversions return field s value prepared for interacting with the database backend used by the default implementations of get_db_prep_save return field s value prepared for saving into a database return a boolean of whether this field has a default value return the default value for this field return empty string return choices with a default blank choices included for use as select choices for this field return a string value of this field from the passed obj this is used by the serialization framework flattened version of choices tuple return a django forms field instance for this field fields with choices get special treatment many of the subclass specific formfield arguments min_value max_value don t apply for choice fields so be sure to only pass the values that typedchoicefield will understand return the value of this field in the given model instance 1 0 are equal to true false bool converts former to latter in html checkboxes required means must be checked which is different from the choices case must select some value required false allows unchecked checkboxes passing max_length to forms charfield means that the value s length will be validated twice this is considered acceptable since we want the value in the form field to pass into widget for example todo handle multiple backends with different feature flags auto_now auto_now_add and default are mutually exclusive options the use of more than one of these options together will trigger an error concrete subclasses use this in their implementations of _check_fix_default_value check if the given value appears to have been provided as a fixed time value and include a warning in the returned list if it does the value argument must be a date object or aware naive datetime object if now is provided it must be a naive datetime object warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup no explicit date datetime value no checks necessary at this point value is a date object convert aware datetimes to the default time zone before casting them to dates 17742 casts dates into the format expected by the backend __init__ is inherited from datefield warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup no explicit date datetime value no checks necessary for backwards compatibility interpret naive datetimes in local time this won t work during dst change but we can t do much about it so we let the exceptions percolate up the call stack contribute_to_class is inherited from datefield it registers get_next_by_foo and get_prev_by_foo for backwards compatibility interpret naive datetimes in local time this won t work during dst change but we can t do much about it so we let the exceptions percolate up the call stack casts datetimes into the format expected by the backend store timedelta objects use interval on postgresql interval day to second on oracle and bigint of microseconds on other databases max_length 254 to be compliant with rfcs 3696 and 5321 we do not exclude max_length if it matches default as we want to change the default in future as with charfield this will cause email validation to be performed twice these validators can t be added at field initialization time since they re based on values retrieved from connection return the data type that a related field pointing to this field should use in most cases a foreign key pointing to a positive integer primary key will have an integer column data type but some databases e g mysql have an unsigned integer type in that case related_fields_match_type true the primary key should return its db_type passing max_length to forms charfield means that the value s length will be validated twice this is considered acceptable since we want the value in the form field to pass into widget for example warn that using an actual date or datetime value is probably wrong it s only evaluated on server startup this will not use the right date in the race condition where now is just before the date change and value is just past 0 00 no explicit time datetime value no checks necessary at this point value is a datetime object not usually a good idea to pass in a datetime here it loses information but this can be a side effect of interacting with a database backend e g oracle so we ll be accommodating casts times into the format expected by the backend as with charfield this will cause url validation to be performed twice binary data is serialized as base64 if it s a string it should be base64 encoded data metaclass to maintain backward inheritance compatibility for autofield it is intended that autofieldmixin become public api when it is possible to create a non integer automatically generated field using column defaults stored in the database in many areas django also relies on using isinstance to check for an automatically generated field as a subclass of autofield a new flag needs to be implemented on field to be used instead when these issues have been addressed this metaclass could be used to deprecate inheritance from autofield and use of isinstance with autofield for detecting automatically generated fields
import copy
import datetime
import decimal
import operator
import uuid
import warnings
from base64 import b64decode, b64encode
from collections.abc import Iterable
from functools import partialmethod, total_ordering

from django import forms
from django.apps import apps
from django.conf import settings
from django.core import checks, exceptions, validators
from django.db import connection, connections, router
from django.db.models.constants import LOOKUP_SEP
from django.db.models.query_utils import DeferredAttribute, RegisterLookupMixin
from django.utils import timezone
from django.utils.choices import (
    BlankChoiceIterator,
    CallableChoiceIterator,
    flatten_choices,
    normalize_choices,
)
from django.utils.datastructures import DictWrapper
from django.utils.dateparse import (
    parse_date,
    parse_datetime,
    parse_duration,
    parse_time,
)
from django.utils.duration import duration_microseconds, duration_string
from django.utils.functional import Promise, cached_property
from django.utils.ipv6 import clean_ipv6_address
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _

__all__ = [
    "AutoField",
    "BLANK_CHOICE_DASH",
    "BigAutoField",
    "BigIntegerField",
    "BinaryField",
    "BooleanField",
    "CharField",
    "CommaSeparatedIntegerField",
    "DateField",
    "DateTimeField",
    "DecimalField",
    "DurationField",
    "EmailField",
    "Empty",
    "Field",
    "FilePathField",
    "FloatField",
    "GenericIPAddressField",
    "IPAddressField",
    "IntegerField",
    "NOT_PROVIDED",
    "NullBooleanField",
    "PositiveBigIntegerField",
    "PositiveIntegerField",
    "PositiveSmallIntegerField",
    "SlugField",
    "SmallAutoField",
    "SmallIntegerField",
    "TextField",
    "TimeField",
    "URLField",
    "UUIDField",
]


class Empty:
    pass


class NOT_PROVIDED:
    pass




BLANK_CHOICE_DASH = [("", "---------")]


def _load_field(app_label, model_name, field_name):
    return apps.get_model(app_label, model_name)._meta.get_field(field_name)


















def _empty(of_cls):
    new = Empty()
    new.__class__ = of_cls
    return new


def return_None():
    return None


@total_ordering
class Field(RegisterLookupMixin):
    

    
    
    empty_strings_allowed = True
    empty_values = list(validators.EMPTY_VALUES)

    
    
    
    creation_counter = 0
    auto_creation_counter = -1
    default_validators = []  
    default_error_messages = {
        "invalid_choice": _("Value %(value)r is not a valid choice."),
        "null": _("This field cannot be null."),
        "blank": _("This field cannot be blank."),
        "unique": _("%(model_name)s with this %(field_label)s already exists."),
        "unique_for_date": _(
            
            
            "%(field_label)s must be unique for "
            "%(date_field_label)s %(lookup_type)s."
        ),
    }
    system_check_deprecated_details = None
    system_check_removed_details = None

    
    
    non_db_attrs = (
        "blank",
        "choices",
        "db_column",
        "editable",
        "error_messages",
        "help_text",
        "limit_choices_to",
        
        "on_delete",
        "related_name",
        "related_query_name",
        "validators",
        "verbose_name",
    )

    
    hidden = False

    many_to_many = None
    many_to_one = None
    one_to_many = None
    one_to_one = None
    related_model = None
    generated = False

    descriptor_class = DeferredAttribute

    
    def _description(self):
        return _("Field of type: %(field_type)s") % {
            "field_type": self.__class__.__name__
        }

    description = property(_description)

    def __init__(
        self,
        verbose_name=None,
        name=None,
        primary_key=False,
        max_length=None,
        unique=False,
        blank=False,
        null=False,
        db_index=False,
        rel=None,
        default=NOT_PROVIDED,
        editable=True,
        serialize=True,
        unique_for_date=None,
        unique_for_month=None,
        unique_for_year=None,
        choices=None,
        help_text="",
        db_column=None,
        db_tablespace=None,
        auto_created=False,
        validators=(),
        error_messages=None,
        db_comment=None,
        db_default=NOT_PROVIDED,
    ):
        self.name = name
        self.verbose_name = verbose_name  
        self._verbose_name = verbose_name  
        self.primary_key = primary_key
        self.max_length, self._unique = max_length, unique
        self.blank, self.null = blank, null
        self.remote_field = rel
        self.is_relation = self.remote_field is not None
        self.default = default
        if db_default is not NOT_PROVIDED and not hasattr(
            db_default, "resolve_expression"
        ):
            from django.db.models.expressions import Value

            db_default = Value(db_default)
        self.db_default = db_default
        self.editable = editable
        self.serialize = serialize
        self.unique_for_date = unique_for_date
        self.unique_for_month = unique_for_month
        self.unique_for_year = unique_for_year
        self.choices = choices
        self.help_text = help_text
        self.db_index = db_index
        self.db_column = db_column
        self.db_comment = db_comment
        self._db_tablespace = db_tablespace
        self.auto_created = auto_created

        
        if auto_created:
            self.creation_counter = Field.auto_creation_counter
            Field.auto_creation_counter -= 1
        else:
            self.creation_counter = Field.creation_counter
            Field.creation_counter += 1

        self._validators = list(validators)  

        self._error_messages = error_messages  

    def __str__(self):
        
        if not hasattr(self, "model"):
            return super().__str__()
        model = self.model
        return "%s.%s" % (model._meta.label, self.name)

    def __repr__(self):
        
        path = "%s.%s" % (self.__class__.__module__, self.__class__.__qualname__)
        name = getattr(self, "name", None)
        if name is not None:
            return "<%s: %s>" % (path, name)
        return "<%s>" % path

    def check(self, **kwargs):
        return [
            *self._check_field_name(),
            *self._check_choices(),
            *self._check_db_default(**kwargs),
            *self._check_db_index(),
            *self._check_db_comment(**kwargs),
            *self._check_null_allowed_for_primary_keys(),
            *self._check_backend_specific_checks(**kwargs),
            *self._check_validators(),
            *self._check_deprecation_details(),
        ]

    def _check_field_name(self):
        
        if self.name is None:
            return []
        if self.name.endswith("_"):
            return [
                checks.Error(
                    "Field names must not end with an underscore.",
                    obj=self,
                    id="fields.E001",
                )
            ]
        elif LOOKUP_SEP in self.name:
            return [
                checks.Error(
                    'Field names must not contain "%s".' % LOOKUP_SEP,
                    obj=self,
                    id="fields.E002",
                )
            ]
        elif self.name == "pk":
            return [
                checks.Error(
                    "'pk' is a reserved word that cannot be used as a field name.",
                    obj=self,
                    id="fields.E003",
                )
            ]
        else:
            return []

    @classmethod
    def _choices_is_value(cls, value):
        return isinstance(value, (str, Promise)) or not isinstance(value, Iterable)

    def _check_choices(self):
        if not self.choices:
            return []

        if not isinstance(self.choices, Iterable) or isinstance(self.choices, str):
            return [
                checks.Error(
                    "'choices' must be a mapping (e.g. a dictionary) or an iterable "
                    "(e.g. a list or tuple).",
                    obj=self,
                    id="fields.E004",
                )
            ]

        choice_max_length = 0
        
        for choices_group in self.choices:
            try:
                group_name, group_choices = choices_group
            except (TypeError, ValueError):
                
                break
            try:
                if not all(
                    self._choices_is_value(value) and self._choices_is_value(human_name)
                    for value, human_name in group_choices
                ):
                    break
                if self.max_length is not None and group_choices:
                    choice_max_length = max(
                        [
                            choice_max_length,
                            *(
                                len(value)
                                for value, _ in group_choices
                                if isinstance(value, str)
                            ),
                        ]
                    )
            except (TypeError, ValueError):
                
                value, human_name = group_name, group_choices
                if not self._choices_is_value(value) or not self._choices_is_value(
                    human_name
                ):
                    break
                if self.max_length is not None and isinstance(value, str):
                    choice_max_length = max(choice_max_length, len(value))

            
            if isinstance(choices_group, str):
                break
        else:
            if self.max_length is not None and choice_max_length > self.max_length:
                return [
                    checks.Error(
                        "'max_length' is too small to fit the longest value "
                        "in 'choices' (%d characters)." % choice_max_length,
                        obj=self,
                        id="fields.E009",
                    ),
                ]
            return []

        return [
            checks.Error(
                "'choices' must be a mapping of actual values to human readable names "
                "or an iterable containing (actual value, human readable name) tuples.",
                obj=self,
                id="fields.E005",
            )
        ]

    def _check_db_default(self, databases=None, **kwargs):
        from django.db.models.expressions import Value

        if (
            self.db_default is NOT_PROVIDED
            or isinstance(self.db_default, Value)
            or databases is None
        ):
            return []
        errors = []
        for db in databases:
            if not router.allow_migrate_model(db, self.model):
                continue
            connection = connections[db]

            if not getattr(self.db_default, "allowed_default", False) and (
                connection.features.supports_expression_defaults
            ):
                msg = f"{self.db_default} cannot be used in db_default."
                errors.append(checks.Error(msg, obj=self, id="fields.E012"))

            if not (
                connection.features.supports_expression_defaults
                or "supports_expression_defaults"
                in self.model._meta.required_db_features
            ):
                msg = (
                    f"{connection.display_name} does not support default database "
                    "values with expressions (db_default)."
                )
                errors.append(checks.Error(msg, obj=self, id="fields.E011"))
        return errors

    def _check_db_index(self):
        if self.db_index not in (None, True, False):
            return [
                checks.Error(
                    "'db_index' must be None, True or False.",
                    obj=self,
                    id="fields.E006",
                )
            ]
        else:
            return []

    def _check_db_comment(self, databases=None, **kwargs):
        if not self.db_comment or not databases:
            return []
        errors = []
        for db in databases:
            if not router.allow_migrate_model(db, self.model):
                continue
            connection = connections[db]
            if not (
                connection.features.supports_comments
                or "supports_comments" in self.model._meta.required_db_features
            ):
                errors.append(
                    checks.Warning(
                        f"{connection.display_name} does not support comments on "
                        f"columns (db_comment).",
                        obj=self,
                        id="fields.W163",
                    )
                )
        return errors

    def _check_null_allowed_for_primary_keys(self):
        if (
            self.primary_key
            and self.null
            and not connection.features.interprets_empty_strings_as_nulls
        ):
            
            
            
            return [
                checks.Error(
                    "Primary keys must not have null=True.",
                    hint=(
                        "Set null=False on the field, or "
                        "remove primary_key=True argument."
                    ),
                    obj=self,
                    id="fields.E007",
                )
            ]
        else:
            return []

    def _check_backend_specific_checks(self, databases=None, **kwargs):
        if databases is None:
            return []
        errors = []
        for alias in databases:
            if router.allow_migrate_model(alias, self.model):
                errors.extend(connections[alias].validation.check_field(self, **kwargs))
        return errors

    def _check_validators(self):
        errors = []
        for i, validator in enumerate(self.validators):
            if not callable(validator):
                errors.append(
                    checks.Error(
                        "All 'validators' must be callable.",
                        hint=(
                            "validators[{i}] ({repr}) isn't a function or "
                            "instance of a validator class.".format(
                                i=i,
                                repr=repr(validator),
                            )
                        ),
                        obj=self,
                        id="fields.E008",
                    )
                )
        return errors

    def _check_deprecation_details(self):
        if self.system_check_removed_details is not None:
            return [
                checks.Error(
                    self.system_check_removed_details.get(
                        "msg",
                        "%s has been removed except for support in historical "
                        "migrations." % self.__class__.__name__,
                    ),
                    hint=self.system_check_removed_details.get("hint"),
                    obj=self,
                    id=self.system_check_removed_details.get("id", "fields.EXXX"),
                )
            ]
        elif self.system_check_deprecated_details is not None:
            return [
                checks.Warning(
                    self.system_check_deprecated_details.get(
                        "msg", "%s has been deprecated." % self.__class__.__name__
                    ),
                    hint=self.system_check_deprecated_details.get("hint"),
                    obj=self,
                    id=self.system_check_deprecated_details.get("id", "fields.WXXX"),
                )
            ]
        return []

    def get_col(self, alias, output_field=None):
        if alias == self.model._meta.db_table and (
            output_field is None or output_field == self
        ):
            return self.cached_col
        from django.db.models.expressions import Col

        return Col(alias, self, output_field)

    @property
    def choices(self):
        return self._choices

    @choices.setter
    def choices(self, value):
        self._choices = normalize_choices(value)

    @cached_property
    def cached_col(self):
        from django.db.models.expressions import Col

        return Col(self.model._meta.db_table, self)

    def select_format(self, compiler, sql, params):
        
        return sql, params

    def deconstruct(self):
        
        
        keywords = {}
        possibles = {
            "verbose_name": None,
            "primary_key": False,
            "max_length": None,
            "unique": False,
            "blank": False,
            "null": False,
            "db_index": False,
            "default": NOT_PROVIDED,
            "db_default": NOT_PROVIDED,
            "editable": True,
            "serialize": True,
            "unique_for_date": None,
            "unique_for_month": None,
            "unique_for_year": None,
            "choices": None,
            "help_text": "",
            "db_column": None,
            "db_comment": None,
            "db_tablespace": None,
            "auto_created": False,
            "validators": [],
            "error_messages": None,
        }
        attr_overrides = {
            "unique": "_unique",
            "error_messages": "_error_messages",
            "validators": "_validators",
            "verbose_name": "_verbose_name",
            "db_tablespace": "_db_tablespace",
        }
        equals_comparison = {"choices", "validators"}
        for name, default in possibles.items():
            value = getattr(self, attr_overrides.get(name, name))
            if isinstance(value, CallableChoiceIterator):
                value = value.func
            
            if name in equals_comparison:
                if value != default:
                    keywords[name] = value
            else:
                if value is not default:
                    keywords[name] = value
        
        path = "%s.%s" % (self.__class__.__module__, self.__class__.__qualname__)
        if path.startswith("django.db.models.fields.related"):
            path = path.replace("django.db.models.fields.related", "django.db.models")
        elif path.startswith("django.db.models.fields.files"):
            path = path.replace("django.db.models.fields.files", "django.db.models")
        elif path.startswith("django.db.models.fields.generated"):
            path = path.replace("django.db.models.fields.generated", "django.db.models")
        elif path.startswith("django.db.models.fields.json"):
            path = path.replace("django.db.models.fields.json", "django.db.models")
        elif path.startswith("django.db.models.fields.proxy"):
            path = path.replace("django.db.models.fields.proxy", "django.db.models")
        elif path.startswith("django.db.models.fields"):
            path = path.replace("django.db.models.fields", "django.db.models")
        
        return (self.name, path, [], keywords)

    def clone(self):
        
        name, path, args, kwargs = self.deconstruct()
        return self.__class__(*args, **kwargs)

    def __eq__(self, other):
        
        if isinstance(other, Field):
            return self.creation_counter == other.creation_counter and getattr(
                self, "model", None
            ) == getattr(other, "model", None)
        return NotImplemented

    def __lt__(self, other):
        
        
        if isinstance(other, Field):
            if (
                self.creation_counter != other.creation_counter
                or not hasattr(self, "model")
                and not hasattr(other, "model")
            ):
                return self.creation_counter < other.creation_counter
            elif hasattr(self, "model") != hasattr(other, "model"):
                return not hasattr(self, "model")  
            else:
                
                return (self.model._meta.app_label, self.model._meta.model_name) < (
                    other.model._meta.app_label,
                    other.model._meta.model_name,
                )
        return NotImplemented

    def __hash__(self):
        return hash(self.creation_counter)

    def __deepcopy__(self, memodict):
        
        
        obj = copy.copy(self)
        if self.remote_field:
            obj.remote_field = copy.copy(self.remote_field)
            if hasattr(self.remote_field, "field") and self.remote_field.field is self:
                obj.remote_field.field = obj
        memodict[id(self)] = obj
        return obj

    def __copy__(self):
        
        
        obj = Empty()
        obj.__class__ = self.__class__
        obj.__dict__ = self.__dict__.copy()
        return obj

    def __reduce__(self):
        
        if not hasattr(self, "model"):
            
            
            
            
            
            state = self.__dict__.copy()
            
            
            state.pop("_get_default", None)
            return _empty, (self.__class__,), state
        return _load_field, (
            self.model._meta.app_label,
            self.model._meta.object_name,
            self.name,
        )

    def get_pk_value_on_save(self, instance):
        
        if self.default:
            return self.get_default()
        return None

    def to_python(self, value):
        
        return value

    @cached_property
    def error_messages(self):
        messages = {}
        for c in reversed(self.__class__.__mro__):
            messages.update(getattr(c, "default_error_messages", {}))
        messages.update(self._error_messages or {})
        return messages

    @cached_property
    def validators(self):
        
        return [*self.default_validators, *self._validators]

    def run_validators(self, value):
        if value in self.empty_values:
            return

        errors = []
        for v in self.validators:
            try:
                v(value)
            except exceptions.ValidationError as e:
                if hasattr(e, "code") and e.code in self.error_messages:
                    e.message = self.error_messages[e.code]
                errors.extend(e.error_list)

        if errors:
            raise exceptions.ValidationError(errors)

    def validate(self, value, model_instance):
        
        if not self.editable:
            
            return

        if self.choices is not None and value not in self.empty_values:
            for option_key, option_value in self.choices:
                if isinstance(option_value, (list, tuple)):
                    
                    
                    for optgroup_key, optgroup_value in option_value:
                        if value == optgroup_key:
                            return
                elif value == option_key:
                    return
            raise exceptions.ValidationError(
                self.error_messages["invalid_choice"],
                code="invalid_choice",
                params={"value": value},
            )

        if value is None and not self.null:
            raise exceptions.ValidationError(self.error_messages["null"], code="null")

        if not self.blank and value in self.empty_values:
            raise exceptions.ValidationError(self.error_messages["blank"], code="blank")

    def clean(self, value, model_instance):
        
        value = self.to_python(value)
        self.validate(value, model_instance)
        self.run_validators(value)
        return value

    def db_type_parameters(self, connection):
        return DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")

    def db_check(self, connection):
        
        data = self.db_type_parameters(connection)
        try:
            return (
                connection.data_type_check_constraints[self.get_internal_type()] % data
            )
        except KeyError:
            return None

    def db_type(self, connection):
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        data = self.db_type_parameters(connection)
        try:
            column_type = connection.data_types[self.get_internal_type()]
        except KeyError:
            return None
        else:
            
            if callable(column_type):
                return column_type(data)
            return column_type % data

    def rel_db_type(self, connection):
        
        return self.db_type(connection)

    def cast_db_type(self, connection):
        
        db_type = connection.ops.cast_data_types.get(self.get_internal_type())
        if db_type:
            return db_type % self.db_type_parameters(connection)
        return self.db_type(connection)

    def db_parameters(self, connection):
        
        type_string = self.db_type(connection)
        check_string = self.db_check(connection)
        return {
            "type": type_string,
            "check": check_string,
        }

    def db_type_suffix(self, connection):
        return connection.data_types_suffix.get(self.get_internal_type())

    def get_db_converters(self, connection):
        if hasattr(self, "from_db_value"):
            return [self.from_db_value]
        return []

    @property
    def unique(self):
        return self._unique or self.primary_key

    @property
    def db_tablespace(self):
        return self._db_tablespace or settings.DEFAULT_INDEX_TABLESPACE

    @property
    def db_returning(self):
        
        return (
            self.db_default is not NOT_PROVIDED
            and connection.features.can_return_columns_from_insert
        )

    def set_attributes_from_name(self, name):
        self.name = self.name or name
        self.attname, self.column = self.get_attname_column()
        self.concrete = self.column is not None
        if self.verbose_name is None and self.name:
            self.verbose_name = self.name.replace("_", " ")

    def contribute_to_class(self, cls, name, private_only=False):
        
        self.set_attributes_from_name(name)
        self.model = cls
        cls._meta.add_field(self, private=private_only)
        if self.column:
            setattr(cls, self.attname, self.descriptor_class(self))
        if self.choices is not None:
            
            
            
            
            if "get_%s_display" % self.name not in cls.__dict__:
                setattr(
                    cls,
                    "get_%s_display" % self.name,
                    partialmethod(cls._get_FIELD_display, field=self),
                )

    def get_filter_kwargs_for_object(self, obj):
        
        return {self.name: getattr(obj, self.attname)}

    def get_attname(self):
        return self.name

    def get_attname_column(self):
        attname = self.get_attname()
        column = self.db_column or attname
        return attname, column

    def get_internal_type(self):
        return self.__class__.__name__

    def pre_save(self, model_instance, add):
        
        value = getattr(model_instance, self.attname)
        if not connection.features.supports_default_keyword_in_insert:
            from django.db.models.expressions import DatabaseDefault

            if isinstance(value, DatabaseDefault):
                return self.db_default
        return value

    def get_prep_value(self, value):
        
        if isinstance(value, Promise):
            value = value._proxy____cast()
        return value

    def get_db_prep_value(self, value, connection, prepared=False):
        
        if not prepared:
            value = self.get_prep_value(value)
        return value

    def get_db_prep_save(self, value, connection):
        
        if hasattr(value, "as_sql"):
            return value
        return self.get_db_prep_value(value, connection=connection, prepared=False)

    def has_default(self):
        
        return self.default is not NOT_PROVIDED

    def get_default(self):
        
        return self._get_default()

    @cached_property
    def _get_default(self):
        if self.has_default():
            if callable(self.default):
                return self.default
            return lambda: self.default

        if self.db_default is not NOT_PROVIDED:
            from django.db.models.expressions import DatabaseDefault

            return DatabaseDefault

        if (
            not self.empty_strings_allowed
            or self.null
            and not connection.features.interprets_empty_strings_as_nulls
        ):
            return return_None
        return str  

    def get_choices(
        self,
        include_blank=True,
        blank_choice=BLANK_CHOICE_DASH,
        limit_choices_to=None,
        ordering=(),
    ):
        
        if self.choices is not None:
            if include_blank:
                return BlankChoiceIterator(self.choices, blank_choice)
            return self.choices
        rel_model = self.remote_field.model
        limit_choices_to = limit_choices_to or self.get_limit_choices_to()
        choice_func = operator.attrgetter(
            self.remote_field.get_related_field().attname
            if hasattr(self.remote_field, "get_related_field")
            else "pk"
        )
        qs = rel_model._default_manager.complex_filter(limit_choices_to)
        if ordering:
            qs = qs.order_by(*ordering)
        return (blank_choice if include_blank else []) + [
            (choice_func(x), str(x)) for x in qs
        ]

    def value_to_string(self, obj):
        
        return str(self.value_from_object(obj))

    @property
    def flatchoices(self):
        
        return list(flatten_choices(self.choices))

    def save_form_data(self, instance, data):
        setattr(instance, self.name, data)

    def formfield(self, form_class=None, choices_form_class=None, **kwargs):
        
        defaults = {
            "required": not self.blank,
            "label": capfirst(self.verbose_name),
            "help_text": self.help_text,
        }
        if self.has_default():
            if callable(self.default):
                defaults["initial"] = self.default
                defaults["show_hidden_initial"] = True
            else:
                defaults["initial"] = self.get_default()
        if self.choices is not None:
            
            include_blank = self.blank or not (
                self.has_default() or "initial" in kwargs
            )
            defaults["choices"] = self.get_choices(include_blank=include_blank)
            defaults["coerce"] = self.to_python
            if self.null:
                defaults["empty_value"] = None
            if choices_form_class is not None:
                form_class = choices_form_class
            else:
                form_class = forms.TypedChoiceField
            
            
            
            for k in list(kwargs):
                if k not in (
                    "coerce",
                    "empty_value",
                    "choices",
                    "required",
                    "widget",
                    "label",
                    "initial",
                    "help_text",
                    "error_messages",
                    "show_hidden_initial",
                    "disabled",
                ):
                    del kwargs[k]
        defaults.update(kwargs)
        if form_class is None:
            form_class = forms.CharField
        return form_class(**defaults)

    def value_from_object(self, obj):
        
        return getattr(obj, self.attname)


class BooleanField(Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _("“%(value)s” value must be either True or False."),
        "invalid_nullable": _("“%(value)s” value must be either True, False, or None."),
    }
    description = _("Boolean (Either True or False)")

    def get_internal_type(self):
        return "BooleanField"

    def to_python(self, value):
        if self.null and value in self.empty_values:
            return None
        if value in (True, False):
            
            return bool(value)
        if value in ("t", "True", "1"):
            return True
        if value in ("f", "False", "0"):
            return False
        raise exceptions.ValidationError(
            self.error_messages["invalid_nullable" if self.null else "invalid"],
            code="invalid",
            params={"value": value},
        )

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        return self.to_python(value)

    def formfield(self, **kwargs):
        if self.choices is not None:
            include_blank = not (self.has_default() or "initial" in kwargs)
            defaults = {"choices": self.get_choices(include_blank=include_blank)}
        else:
            form_class = forms.NullBooleanField if self.null else forms.BooleanField
            
            
            
            defaults = {"form_class": form_class, "required": False}
        return super().formfield(**{**defaults, **kwargs})


class CharField(Field):
    def __init__(self, *args, db_collation=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.db_collation = db_collation
        if self.max_length is not None:
            self.validators.append(validators.MaxLengthValidator(self.max_length))

    @property
    def description(self):
        if self.max_length is not None:
            return _("String (up to %(max_length)s)")
        else:
            return _("String (unlimited)")

    def check(self, **kwargs):
        databases = kwargs.get("databases") or []
        return [
            *super().check(**kwargs),
            *self._check_db_collation(databases),
            *self._check_max_length_attribute(**kwargs),
        ]

    def _check_max_length_attribute(self, **kwargs):
        if self.max_length is None:
            if (
                connection.features.supports_unlimited_charfield
                or "supports_unlimited_charfield"
                in self.model._meta.required_db_features
            ):
                return []
            return [
                checks.Error(
                    "CharFields must define a 'max_length' attribute.",
                    obj=self,
                    id="fields.E120",
                )
            ]
        elif (
            not isinstance(self.max_length, int)
            or isinstance(self.max_length, bool)
            or self.max_length <= 0
        ):
            return [
                checks.Error(
                    "'max_length' must be a positive integer.",
                    obj=self,
                    id="fields.E121",
                )
            ]
        else:
            return []

    def _check_db_collation(self, databases):
        errors = []
        for db in databases:
            if not router.allow_migrate_model(db, self.model):
                continue
            connection = connections[db]
            if not (
                self.db_collation is None
                or "supports_collation_on_charfield"
                in self.model._meta.required_db_features
                or connection.features.supports_collation_on_charfield
            ):
                errors.append(
                    checks.Error(
                        "%s does not support a database collation on "
                        "CharFields." % connection.display_name,
                        obj=self,
                        id="fields.E190",
                    ),
                )
        return errors

    def cast_db_type(self, connection):
        if self.max_length is None:
            return connection.ops.cast_char_field_without_max_length
        return super().cast_db_type(connection)

    def db_parameters(self, connection):
        db_params = super().db_parameters(connection)
        db_params["collation"] = self.db_collation
        return db_params

    def get_internal_type(self):
        return "CharField"

    def to_python(self, value):
        if isinstance(value, str) or value is None:
            return value
        return str(value)

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def formfield(self, **kwargs):
        
        
        
        defaults = {"max_length": self.max_length}
        
        if self.null and not connection.features.interprets_empty_strings_as_nulls:
            defaults["empty_value"] = None
        defaults.update(kwargs)
        return super().formfield(**defaults)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.db_collation:
            kwargs["db_collation"] = self.db_collation
        return name, path, args, kwargs


class CommaSeparatedIntegerField(CharField):
    default_validators = [validators.validate_comma_separated_integer_list]
    description = _("Comma-separated integers")
    system_check_removed_details = {
        "msg": (
            "CommaSeparatedIntegerField is removed except for support in "
            "historical migrations."
        ),
        "hint": (
            "Use CharField(validators=[validate_comma_separated_integer_list]) "
            "instead."
        ),
        "id": "fields.E901",
    }


def _to_naive(value):
    if timezone.is_aware(value):
        value = timezone.make_naive(value, datetime.timezone.utc)
    return value


def _get_naive_now():
    return _to_naive(timezone.now())


class DateTimeCheckMixin:
    def check(self, **kwargs):
        return [
            *super().check(**kwargs),
            *self._check_mutually_exclusive_options(),
            *self._check_fix_default_value(),
        ]

    def _check_mutually_exclusive_options(self):
        
        
        
        mutually_exclusive_options = [
            self.auto_now_add,
            self.auto_now,
            self.has_default(),
        ]
        enabled_options = [
            option not in (None, False) for option in mutually_exclusive_options
        ].count(True)
        if enabled_options > 1:
            return [
                checks.Error(
                    "The options auto_now, auto_now_add, and default "
                    "are mutually exclusive. Only one of these options "
                    "may be present.",
                    obj=self,
                    id="fields.E160",
                )
            ]
        else:
            return []

    def _check_fix_default_value(self):
        return []

    
    
    def _check_if_value_fixed(self, value, now=None):
        
        if now is None:
            now = _get_naive_now()
        offset = datetime.timedelta(seconds=10)
        lower = now - offset
        upper = now + offset
        if isinstance(value, datetime.datetime):
            value = _to_naive(value)
        else:
            assert isinstance(value, datetime.date)
            lower = lower.date()
            upper = upper.date()
        if lower <= value <= upper:
            return [
                checks.Warning(
                    "Fixed default value provided.",
                    hint=(
                        "It seems you set a fixed date / time / datetime "
                        "value as default for this field. This may not be "
                        "what you want. If you want to have the current date "
                        "as default, use `django.utils.timezone.now`"
                    ),
                    obj=self,
                    id="fields.W161",
                )
            ]
        return []


class DateField(DateTimeCheckMixin, Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _(
            "“%(value)s” value has an invalid date format. It must be "
            "in YYYY-MM-DD format."
        ),
        "invalid_date": _(
            "“%(value)s” value has the correct format (YYYY-MM-DD) "
            "but it is an invalid date."
        ),
    }
    description = _("Date (without time)")

    def __init__(
        self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs
    ):
        self.auto_now, self.auto_now_add = auto_now, auto_now_add
        if auto_now or auto_now_add:
            kwargs["editable"] = False
            kwargs["blank"] = True
        super().__init__(verbose_name, name, **kwargs)

    def _check_fix_default_value(self):
        
        if not self.has_default():
            return []

        value = self.default
        if isinstance(value, datetime.datetime):
            value = _to_naive(value).date()
        elif isinstance(value, datetime.date):
            pass
        else:
            
            return []
        
        return self._check_if_value_fixed(value)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.auto_now:
            kwargs["auto_now"] = True
        if self.auto_now_add:
            kwargs["auto_now_add"] = True
        if self.auto_now or self.auto_now_add:
            del kwargs["editable"]
            del kwargs["blank"]
        return name, path, args, kwargs

    def get_internal_type(self):
        return "DateField"

    def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, datetime.datetime):
            if settings.USE_TZ and timezone.is_aware(value):
                
                
                default_timezone = timezone.get_default_timezone()
                value = timezone.make_naive(value, default_timezone)
            return value.date()
        if isinstance(value, datetime.date):
            return value

        try:
            parsed = parse_date(value)
            if parsed is not None:
                return parsed
        except ValueError:
            raise exceptions.ValidationError(
                self.error_messages["invalid_date"],
                code="invalid_date",
                params={"value": value},
            )

        raise exceptions.ValidationError(
            self.error_messages["invalid"],
            code="invalid",
            params={"value": value},
        )

    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = datetime.date.today()
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super().pre_save(model_instance, add)

    def contribute_to_class(self, cls, name, **kwargs):
        super().contribute_to_class(cls, name, **kwargs)
        if not self.null:
            setattr(
                cls,
                "get_next_by_%s" % self.name,
                partialmethod(
                    cls._get_next_or_previous_by_FIELD, field=self, is_next=True
                ),
            )
            setattr(
                cls,
                "get_previous_by_%s" % self.name,
                partialmethod(
                    cls._get_next_or_previous_by_FIELD, field=self, is_next=False
                ),
            )

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def get_db_prep_value(self, value, connection, prepared=False):
        
        if not prepared:
            value = self.get_prep_value(value)
        return connection.ops.adapt_datefield_value(value)

    def value_to_string(self, obj):
        val = self.value_from_object(obj)
        return "" if val is None else val.isoformat()

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.DateField,
                **kwargs,
            }
        )


class DateTimeField(DateField):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _(
            "“%(value)s” value has an invalid format. It must be in "
            "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."
        ),
        "invalid_date": _(
            "“%(value)s” value has the correct format "
            "(YYYY-MM-DD) but it is an invalid date."
        ),
        "invalid_datetime": _(
            "“%(value)s” value has the correct format "
            "(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) "
            "but it is an invalid date/time."
        ),
    }
    description = _("Date (with time)")

    

    def _check_fix_default_value(self):
        
        if not self.has_default():
            return []

        value = self.default
        if isinstance(value, (datetime.datetime, datetime.date)):
            return self._check_if_value_fixed(value)
        
        return []

    def get_internal_type(self):
        return "DateTimeField"

    def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, datetime.datetime):
            return value
        if isinstance(value, datetime.date):
            value = datetime.datetime(value.year, value.month, value.day)
            if settings.USE_TZ:
                
                
                
                
                try:
                    name = f"{self.model.__name__}.{self.name}"
                except AttributeError:
                    name = "(unbound)"
                warnings.warn(
                    f"DateTimeField {name} received a naive datetime ({value}) while "
                    "time zone support is active.",
                    RuntimeWarning,
                )
                default_timezone = timezone.get_default_timezone()
                value = timezone.make_aware(value, default_timezone)
            return value

        try:
            parsed = parse_datetime(value)
            if parsed is not None:
                return parsed
        except ValueError:
            raise exceptions.ValidationError(
                self.error_messages["invalid_datetime"],
                code="invalid_datetime",
                params={"value": value},
            )

        try:
            parsed = parse_date(value)
            if parsed is not None:
                return datetime.datetime(parsed.year, parsed.month, parsed.day)
        except ValueError:
            raise exceptions.ValidationError(
                self.error_messages["invalid_date"],
                code="invalid_date",
                params={"value": value},
            )

        raise exceptions.ValidationError(
            self.error_messages["invalid"],
            code="invalid",
            params={"value": value},
        )

    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = timezone.now()
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super().pre_save(model_instance, add)

    
    

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        value = self.to_python(value)
        if value is not None and settings.USE_TZ and timezone.is_naive(value):
            
            
            
            try:
                name = "%s.%s" % (self.model.__name__, self.name)
            except AttributeError:
                name = "(unbound)"
            warnings.warn(
                "DateTimeField %s received a naive datetime (%s)"
                " while time zone support is active." % (name, value),
                RuntimeWarning,
            )
            default_timezone = timezone.get_default_timezone()
            value = timezone.make_aware(value, default_timezone)
        return value

    def get_db_prep_value(self, value, connection, prepared=False):
        
        if not prepared:
            value = self.get_prep_value(value)
        return connection.ops.adapt_datetimefield_value(value)

    def value_to_string(self, obj):
        val = self.value_from_object(obj)
        return "" if val is None else val.isoformat()

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.DateTimeField,
                **kwargs,
            }
        )


class DecimalField(Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _("“%(value)s” value must be a decimal number."),
    }
    description = _("Decimal number")

    def __init__(
        self,
        verbose_name=None,
        name=None,
        max_digits=None,
        decimal_places=None,
        **kwargs,
    ):
        self.max_digits, self.decimal_places = max_digits, decimal_places
        super().__init__(verbose_name, name, **kwargs)

    def check(self, **kwargs):
        errors = super().check(**kwargs)

        digits_errors = [
            *self._check_decimal_places(),
            *self._check_max_digits(),
        ]
        if not digits_errors:
            errors.extend(self._check_decimal_places_and_max_digits(**kwargs))
        else:
            errors.extend(digits_errors)
        return errors

    def _check_decimal_places(self):
        try:
            decimal_places = int(self.decimal_places)
            if decimal_places < 0:
                raise ValueError()
        except TypeError:
            return [
                checks.Error(
                    "DecimalFields must define a 'decimal_places' attribute.",
                    obj=self,
                    id="fields.E130",
                )
            ]
        except ValueError:
            return [
                checks.Error(
                    "'decimal_places' must be a non-negative integer.",
                    obj=self,
                    id="fields.E131",
                )
            ]
        else:
            return []

    def _check_max_digits(self):
        try:
            max_digits = int(self.max_digits)
            if max_digits <= 0:
                raise ValueError()
        except TypeError:
            return [
                checks.Error(
                    "DecimalFields must define a 'max_digits' attribute.",
                    obj=self,
                    id="fields.E132",
                )
            ]
        except ValueError:
            return [
                checks.Error(
                    "'max_digits' must be a positive integer.",
                    obj=self,
                    id="fields.E133",
                )
            ]
        else:
            return []

    def _check_decimal_places_and_max_digits(self, **kwargs):
        if int(self.decimal_places) > int(self.max_digits):
            return [
                checks.Error(
                    "'max_digits' must be greater or equal to 'decimal_places'.",
                    obj=self,
                    id="fields.E134",
                )
            ]
        return []

    @cached_property
    def validators(self):
        return super().validators + [
            validators.DecimalValidator(self.max_digits, self.decimal_places)
        ]

    @cached_property
    def context(self):
        return decimal.Context(prec=self.max_digits)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.max_digits is not None:
            kwargs["max_digits"] = self.max_digits
        if self.decimal_places is not None:
            kwargs["decimal_places"] = self.decimal_places
        return name, path, args, kwargs

    def get_internal_type(self):
        return "DecimalField"

    def to_python(self, value):
        if value is None:
            return value
        try:
            if isinstance(value, float):
                decimal_value = self.context.create_decimal_from_float(value)
            else:
                decimal_value = decimal.Decimal(value)
        except (decimal.InvalidOperation, TypeError, ValueError):
            raise exceptions.ValidationError(
                self.error_messages["invalid"],
                code="invalid",
                params={"value": value},
            )
        if not decimal_value.is_finite():
            raise exceptions.ValidationError(
                self.error_messages["invalid"],
                code="invalid",
                params={"value": value},
            )
        return decimal_value

    def get_db_prep_save(self, value, connection):
        if hasattr(value, "as_sql"):
            return value
        return connection.ops.adapt_decimalfield_value(
            self.to_python(value), self.max_digits, self.decimal_places
        )

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "max_digits": self.max_digits,
                "decimal_places": self.decimal_places,
                "form_class": forms.DecimalField,
                **kwargs,
            }
        )


class DurationField(Field):
    

    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _(
            "“%(value)s” value has an invalid format. It must be in "
            "[DD] [[HH:]MM:]ss[.uuuuuu] format."
        )
    }
    description = _("Duration")

    def get_internal_type(self):
        return "DurationField"

    def to_python(self, value):
        if value is None:
            return value
        if isinstance(value, datetime.timedelta):
            return value
        try:
            parsed = parse_duration(value)
        except ValueError:
            pass
        else:
            if parsed is not None:
                return parsed

        raise exceptions.ValidationError(
            self.error_messages["invalid"],
            code="invalid",
            params={"value": value},
        )

    def get_db_prep_value(self, value, connection, prepared=False):
        if connection.features.has_native_duration_field:
            return value
        if value is None:
            return None
        return duration_microseconds(value)

    def get_db_converters(self, connection):
        converters = []
        if not connection.features.has_native_duration_field:
            converters.append(connection.ops.convert_durationfield_value)
        return converters + super().get_db_converters(connection)

    def value_to_string(self, obj):
        val = self.value_from_object(obj)
        return "" if val is None else duration_string(val)

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.DurationField,
                **kwargs,
            }
        )


class EmailField(CharField):
    default_validators = [validators.validate_email]
    description = _("Email address")

    def __init__(self, *args, **kwargs):
        
        kwargs.setdefault("max_length", 254)
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        
        
        return name, path, args, kwargs

    def formfield(self, **kwargs):
        
        
        return super().formfield(
            **{
                "form_class": forms.EmailField,
                **kwargs,
            }
        )


class FilePathField(Field):
    description = _("File path")

    def __init__(
        self,
        verbose_name=None,
        name=None,
        path="",
        match=None,
        recursive=False,
        allow_files=True,
        allow_folders=False,
        **kwargs,
    ):
        self.path, self.match, self.recursive = path, match, recursive
        self.allow_files, self.allow_folders = allow_files, allow_folders
        kwargs.setdefault("max_length", 100)
        super().__init__(verbose_name, name, **kwargs)

    def check(self, **kwargs):
        return [
            *super().check(**kwargs),
            *self._check_allowing_files_or_folders(**kwargs),
        ]

    def _check_allowing_files_or_folders(self, **kwargs):
        if not self.allow_files and not self.allow_folders:
            return [
                checks.Error(
                    "FilePathFields must have either 'allow_files' or 'allow_folders' "
                    "set to True.",
                    obj=self,
                    id="fields.E140",
                )
            ]
        return []

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.path != "":
            kwargs["path"] = self.path
        if self.match is not None:
            kwargs["match"] = self.match
        if self.recursive is not False:
            kwargs["recursive"] = self.recursive
        if self.allow_files is not True:
            kwargs["allow_files"] = self.allow_files
        if self.allow_folders is not False:
            kwargs["allow_folders"] = self.allow_folders
        if kwargs.get("max_length") == 100:
            del kwargs["max_length"]
        return name, path, args, kwargs

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        return str(value)

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "path": self.path() if callable(self.path) else self.path,
                "match": self.match,
                "recursive": self.recursive,
                "form_class": forms.FilePathField,
                "allow_files": self.allow_files,
                "allow_folders": self.allow_folders,
                **kwargs,
            }
        )

    def get_internal_type(self):
        return "FilePathField"


class FloatField(Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _("“%(value)s” value must be a float."),
    }
    description = _("Floating point number")

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        try:
            return float(value)
        except (TypeError, ValueError) as e:
            raise e.__class__(
                "Field '%s' expected a number but got %r." % (self.name, value),
            ) from e

    def get_internal_type(self):
        return "FloatField"

    def to_python(self, value):
        if value is None:
            return value
        try:
            return float(value)
        except (TypeError, ValueError):
            raise exceptions.ValidationError(
                self.error_messages["invalid"],
                code="invalid",
                params={"value": value},
            )

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.FloatField,
                **kwargs,
            }
        )


class IntegerField(Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _("“%(value)s” value must be an integer."),
    }
    description = _("Integer")

    def check(self, **kwargs):
        return [
            *super().check(**kwargs),
            *self._check_max_length_warning(),
        ]

    def _check_max_length_warning(self):
        if self.max_length is not None:
            return [
                checks.Warning(
                    "'max_length' is ignored when used with %s."
                    % self.__class__.__name__,
                    hint="Remove 'max_length' from field",
                    obj=self,
                    id="fields.W122",
                )
            ]
        return []

    @cached_property
    def validators(self):
        
        
        validators_ = super().validators
        internal_type = self.get_internal_type()
        min_value, max_value = connection.ops.integer_field_range(internal_type)
        if min_value is not None and not any(
            (
                isinstance(validator, validators.MinValueValidator)
                and (
                    validator.limit_value()
                    if callable(validator.limit_value)
                    else validator.limit_value
                )
                >= min_value
            )
            for validator in validators_
        ):
            validators_.append(validators.MinValueValidator(min_value))
        if max_value is not None and not any(
            (
                isinstance(validator, validators.MaxValueValidator)
                and (
                    validator.limit_value()
                    if callable(validator.limit_value)
                    else validator.limit_value
                )
                <= max_value
            )
            for validator in validators_
        ):
            validators_.append(validators.MaxValueValidator(max_value))
        return validators_

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        try:
            return int(value)
        except (TypeError, ValueError) as e:
            raise e.__class__(
                "Field '%s' expected a number but got %r." % (self.name, value),
            ) from e

    def get_db_prep_value(self, value, connection, prepared=False):
        value = super().get_db_prep_value(value, connection, prepared)
        return connection.ops.adapt_integerfield_value(value, self.get_internal_type())

    def get_internal_type(self):
        return "IntegerField"

    def to_python(self, value):
        if value is None:
            return value
        try:
            return int(value)
        except (TypeError, ValueError):
            raise exceptions.ValidationError(
                self.error_messages["invalid"],
                code="invalid",
                params={"value": value},
            )

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.IntegerField,
                **kwargs,
            }
        )


class BigIntegerField(IntegerField):
    description = _("Big (8 byte) integer")
    MAX_BIGINT = 9223372036854775807

    def get_internal_type(self):
        return "BigIntegerField"

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "min_value": -BigIntegerField.MAX_BIGINT - 1,
                "max_value": BigIntegerField.MAX_BIGINT,
                **kwargs,
            }
        )


class SmallIntegerField(IntegerField):
    description = _("Small integer")

    def get_internal_type(self):
        return "SmallIntegerField"


class IPAddressField(Field):
    empty_strings_allowed = False
    description = _("IPv4 address")
    system_check_removed_details = {
        "msg": (
            "IPAddressField has been removed except for support in "
            "historical migrations."
        ),
        "hint": "Use GenericIPAddressField instead.",
        "id": "fields.E900",
    }

    def __init__(self, *args, **kwargs):
        kwargs["max_length"] = 15
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        del kwargs["max_length"]
        return name, path, args, kwargs

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        return str(value)

    def get_internal_type(self):
        return "IPAddressField"


class GenericIPAddressField(Field):
    empty_strings_allowed = False
    description = _("IP address")
    default_error_messages = {"invalid": _("Enter a valid %(protocol)s address.")}

    def __init__(
        self,
        verbose_name=None,
        name=None,
        protocol="both",
        unpack_ipv4=False,
        *args,
        **kwargs,
    ):
        self.unpack_ipv4 = unpack_ipv4
        self.protocol = protocol
        self.default_validators = validators.ip_address_validators(
            protocol, unpack_ipv4
        )
        kwargs["max_length"] = 39
        super().__init__(verbose_name, name, *args, **kwargs)

    def check(self, **kwargs):
        return [
            *super().check(**kwargs),
            *self._check_blank_and_null_values(**kwargs),
        ]

    def _check_blank_and_null_values(self, **kwargs):
        if not getattr(self, "null", False) and getattr(self, "blank", False):
            return [
                checks.Error(
                    "GenericIPAddressFields cannot have blank=True if null=False, "
                    "as blank values are stored as nulls.",
                    obj=self,
                    id="fields.E150",
                )
            ]
        return []

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.unpack_ipv4 is not False:
            kwargs["unpack_ipv4"] = self.unpack_ipv4
        if self.protocol != "both":
            kwargs["protocol"] = self.protocol
        if kwargs.get("max_length") == 39:
            del kwargs["max_length"]
        return name, path, args, kwargs

    def get_internal_type(self):
        return "GenericIPAddressField"

    def to_python(self, value):
        if value is None:
            return None
        if not isinstance(value, str):
            value = str(value)
        value = value.strip()
        if ":" in value:
            return clean_ipv6_address(
                value, self.unpack_ipv4, self.error_messages["invalid"]
            )
        return value

    def get_db_prep_value(self, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_value(value)
        return connection.ops.adapt_ipaddressfield_value(value)

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        if value is None:
            return None
        if value and ":" in value:
            try:
                return clean_ipv6_address(value, self.unpack_ipv4)
            except exceptions.ValidationError:
                pass
        return str(value)

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "protocol": self.protocol,
                "form_class": forms.GenericIPAddressField,
                **kwargs,
            }
        )


class NullBooleanField(BooleanField):
    default_error_messages = {
        "invalid": _("“%(value)s” value must be either None, True or False."),
        "invalid_nullable": _("“%(value)s” value must be either None, True or False."),
    }
    description = _("Boolean (Either True, False or None)")
    system_check_removed_details = {
        "msg": (
            "NullBooleanField is removed except for support in historical "
            "migrations."
        ),
        "hint": "Use BooleanField(null=True, blank=True) instead.",
        "id": "fields.E903",
    }

    def __init__(self, *args, **kwargs):
        kwargs["null"] = True
        kwargs["blank"] = True
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        del kwargs["null"]
        del kwargs["blank"]
        return name, path, args, kwargs


class PositiveIntegerRelDbTypeMixin:
    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if not hasattr(cls, "integer_field_class"):
            cls.integer_field_class = next(
                (
                    parent
                    for parent in cls.__mro__[1:]
                    if issubclass(parent, IntegerField)
                ),
                None,
            )

    def rel_db_type(self, connection):
        
        if connection.features.related_fields_match_type:
            return self.db_type(connection)
        else:
            return self.integer_field_class().db_type(connection=connection)


class PositiveBigIntegerField(PositiveIntegerRelDbTypeMixin, BigIntegerField):
    description = _("Positive big integer")

    def get_internal_type(self):
        return "PositiveBigIntegerField"

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "min_value": 0,
                **kwargs,
            }
        )


class PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField):
    description = _("Positive integer")

    def get_internal_type(self):
        return "PositiveIntegerField"

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "min_value": 0,
                **kwargs,
            }
        )


class PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, SmallIntegerField):
    description = _("Positive small integer")

    def get_internal_type(self):
        return "PositiveSmallIntegerField"

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "min_value": 0,
                **kwargs,
            }
        )


class SlugField(CharField):
    default_validators = [validators.validate_slug]
    description = _("Slug (up to %(max_length)s)")

    def __init__(
        self, *args, max_length=50, db_index=True, allow_unicode=False, **kwargs
    ):
        self.allow_unicode = allow_unicode
        if self.allow_unicode:
            self.default_validators = [validators.validate_unicode_slug]
        super().__init__(*args, max_length=max_length, db_index=db_index, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if kwargs.get("max_length") == 50:
            del kwargs["max_length"]
        if self.db_index is False:
            kwargs["db_index"] = False
        else:
            del kwargs["db_index"]
        if self.allow_unicode is not False:
            kwargs["allow_unicode"] = self.allow_unicode
        return name, path, args, kwargs

    def get_internal_type(self):
        return "SlugField"

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.SlugField,
                "allow_unicode": self.allow_unicode,
                **kwargs,
            }
        )


class TextField(Field):
    description = _("Text")

    def __init__(self, *args, db_collation=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.db_collation = db_collation

    def check(self, **kwargs):
        databases = kwargs.get("databases") or []
        return [
            *super().check(**kwargs),
            *self._check_db_collation(databases),
        ]

    def _check_db_collation(self, databases):
        errors = []
        for db in databases:
            if not router.allow_migrate_model(db, self.model):
                continue
            connection = connections[db]
            if not (
                self.db_collation is None
                or "supports_collation_on_textfield"
                in self.model._meta.required_db_features
                or connection.features.supports_collation_on_textfield
            ):
                errors.append(
                    checks.Error(
                        "%s does not support a database collation on "
                        "TextFields." % connection.display_name,
                        obj=self,
                        id="fields.E190",
                    ),
                )
        return errors

    def db_parameters(self, connection):
        db_params = super().db_parameters(connection)
        db_params["collation"] = self.db_collation
        return db_params

    def get_internal_type(self):
        return "TextField"

    def to_python(self, value):
        if isinstance(value, str) or value is None:
            return value
        return str(value)

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def formfield(self, **kwargs):
        
        
        
        return super().formfield(
            **{
                "max_length": self.max_length,
                **({} if self.choices is not None else {"widget": forms.Textarea}),
                **kwargs,
            }
        )

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.db_collation:
            kwargs["db_collation"] = self.db_collation
        return name, path, args, kwargs


class TimeField(DateTimeCheckMixin, Field):
    empty_strings_allowed = False
    default_error_messages = {
        "invalid": _(
            "“%(value)s” value has an invalid format. It must be in "
            "HH:MM[:ss[.uuuuuu]] format."
        ),
        "invalid_time": _(
            "“%(value)s” value has the correct format "
            "(HH:MM[:ss[.uuuuuu]]) but it is an invalid time."
        ),
    }
    description = _("Time")

    def __init__(
        self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs
    ):
        self.auto_now, self.auto_now_add = auto_now, auto_now_add
        if auto_now or auto_now_add:
            kwargs["editable"] = False
            kwargs["blank"] = True
        super().__init__(verbose_name, name, **kwargs)

    def _check_fix_default_value(self):
        
        if not self.has_default():
            return []

        value = self.default
        if isinstance(value, datetime.datetime):
            now = None
        elif isinstance(value, datetime.time):
            now = _get_naive_now()
            
            
            value = datetime.datetime.combine(now.date(), value)
        else:
            
            return []
        
        return self._check_if_value_fixed(value, now=now)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.auto_now is not False:
            kwargs["auto_now"] = self.auto_now
        if self.auto_now_add is not False:
            kwargs["auto_now_add"] = self.auto_now_add
        if self.auto_now or self.auto_now_add:
            del kwargs["blank"]
            del kwargs["editable"]
        return name, path, args, kwargs

    def get_internal_type(self):
        return "TimeField"

    def to_python(self, value):
        if value is None:
            return None
        if isinstance(value, datetime.time):
            return value
        if isinstance(value, datetime.datetime):
            
            
            
            return value.time()

        try:
            parsed = parse_time(value)
            if parsed is not None:
                return parsed
        except ValueError:
            raise exceptions.ValidationError(
                self.error_messages["invalid_time"],
                code="invalid_time",
                params={"value": value},
            )

        raise exceptions.ValidationError(
            self.error_messages["invalid"],
            code="invalid",
            params={"value": value},
        )

    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = datetime.datetime.now().time()
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super().pre_save(model_instance, add)

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def get_db_prep_value(self, value, connection, prepared=False):
        
        if not prepared:
            value = self.get_prep_value(value)
        return connection.ops.adapt_timefield_value(value)

    def value_to_string(self, obj):
        val = self.value_from_object(obj)
        return "" if val is None else val.isoformat()

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.TimeField,
                **kwargs,
            }
        )


class URLField(CharField):
    default_validators = [validators.URLValidator()]
    description = _("URL")

    def __init__(self, verbose_name=None, name=None, **kwargs):
        kwargs.setdefault("max_length", 200)
        super().__init__(verbose_name, name, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if kwargs.get("max_length") == 200:
            del kwargs["max_length"]
        return name, path, args, kwargs

    def formfield(self, **kwargs):
        
        
        return super().formfield(
            **{
                "form_class": forms.URLField,
                **kwargs,
            }
        )


class BinaryField(Field):
    description = _("Raw binary data")
    empty_values = [None, b""]

    def __init__(self, *args, **kwargs):
        kwargs.setdefault("editable", False)
        super().__init__(*args, **kwargs)
        if self.max_length is not None:
            self.validators.append(validators.MaxLengthValidator(self.max_length))

    def check(self, **kwargs):
        return [*super().check(**kwargs), *self._check_str_default_value()]

    def _check_str_default_value(self):
        if self.has_default() and isinstance(self.default, str):
            return [
                checks.Error(
                    "BinaryField's default cannot be a string. Use bytes "
                    "content instead.",
                    obj=self,
                    id="fields.E170",
                )
            ]
        return []

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if self.editable:
            kwargs["editable"] = True
        else:
            del kwargs["editable"]
        return name, path, args, kwargs

    def get_internal_type(self):
        return "BinaryField"

    def get_placeholder(self, value, compiler, connection):
        return connection.ops.binary_placeholder_sql(value)

    def get_default(self):
        if self.has_default() and not callable(self.default):
            return self.default
        default = super().get_default()
        if default == "":
            return b""
        return default

    def get_db_prep_value(self, value, connection, prepared=False):
        value = super().get_db_prep_value(value, connection, prepared)
        if value is not None:
            return connection.Database.Binary(value)
        return value

    def value_to_string(self, obj):
        
        return b64encode(self.value_from_object(obj)).decode("ascii")

    def to_python(self, value):
        
        if isinstance(value, str):
            return memoryview(b64decode(value.encode("ascii")))
        return value


class UUIDField(Field):
    default_error_messages = {
        "invalid": _("“%(value)s” is not a valid UUID."),
    }
    description = _("Universally unique identifier")
    empty_strings_allowed = False

    def __init__(self, verbose_name=None, **kwargs):
        kwargs["max_length"] = 32
        super().__init__(verbose_name, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        del kwargs["max_length"]
        return name, path, args, kwargs

    def get_internal_type(self):
        return "UUIDField"

    def get_prep_value(self, value):
        value = super().get_prep_value(value)
        return self.to_python(value)

    def get_db_prep_value(self, value, connection, prepared=False):
        if value is None:
            return None
        if not isinstance(value, uuid.UUID):
            value = self.to_python(value)

        if connection.features.has_native_uuid_field:
            return value
        return value.hex

    def to_python(self, value):
        if value is not None and not isinstance(value, uuid.UUID):
            input_form = "int" if isinstance(value, int) else "hex"
            try:
                return uuid.UUID(**{input_form: value})
            except (AttributeError, ValueError):
                raise exceptions.ValidationError(
                    self.error_messages["invalid"],
                    code="invalid",
                    params={"value": value},
                )
        return value

    def formfield(self, **kwargs):
        return super().formfield(
            **{
                "form_class": forms.UUIDField,
                **kwargs,
            }
        )


class AutoFieldMixin:
    db_returning = True

    def __init__(self, *args, **kwargs):
        kwargs["blank"] = True
        super().__init__(*args, **kwargs)

    def check(self, **kwargs):
        return [
            *super().check(**kwargs),
            *self._check_primary_key(),
        ]

    def _check_primary_key(self):
        if not self.primary_key:
            return [
                checks.Error(
                    "AutoFields must set primary_key=True.",
                    obj=self,
                    id="fields.E100",
                ),
            ]
        else:
            return []

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        del kwargs["blank"]
        kwargs["primary_key"] = True
        return name, path, args, kwargs

    def validate(self, value, model_instance):
        pass

    def get_db_prep_value(self, value, connection, prepared=False):
        if not prepared:
            value = self.get_prep_value(value)
            value = connection.ops.validate_autopk_value(value)
        return value

    def contribute_to_class(self, cls, name, **kwargs):
        if cls._meta.auto_field:
            raise ValueError(
                "Model %s can't have more than one auto-generated field."
                % cls._meta.label
            )
        super().contribute_to_class(cls, name, **kwargs)
        cls._meta.auto_field = self

    def formfield(self, **kwargs):
        return None


class AutoFieldMeta(type):
    

    @property
    def _subclasses(self):
        return (BigAutoField, SmallAutoField)

    def __instancecheck__(self, instance):
        return isinstance(instance, self._subclasses) or super().__instancecheck__(
            instance
        )

    def __subclasscheck__(self, subclass):
        return issubclass(subclass, self._subclasses) or super().__subclasscheck__(
            subclass
        )


class AutoField(AutoFieldMixin, IntegerField, metaclass=AutoFieldMeta):
    def get_internal_type(self):
        return "AutoField"

    def rel_db_type(self, connection):
        return IntegerField().db_type(connection=connection)


class BigAutoField(AutoFieldMixin, BigIntegerField):
    def get_internal_type(self):
        return "BigAutoField"

    def rel_db_type(self, connection):
        return BigIntegerField().db_type(connection=connection)


class SmallAutoField(AutoFieldMixin, SmallIntegerField):
    def get_internal_type(self):
        return "SmallAutoField"

    def rel_db_type(self, connection):
        return SmallIntegerField().db_type(connection=connection)