我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.db.models.fields.Field()。
def generate_fields(model, add=None, remove=None): if add is None: add = [] if remove is None: remove = [] remove.append("id") result = [] for field in model._meta.get_fields(): if isinstance(field, Field): result.append(field.name) for item in add: result.append(item) for item in remove: try: result.remove(item) except ValueError: pass return tuple(result)
def get_form(self, req: HttpRequest, obj: Domain=None, **kwargs: Any) -> type: if req.GET.get("_prefill_key", "0") == "1": def formfield_callback(field: _ModelField, request: HttpRequest=None, **kwargs: Any) -> Type[_FormField]: f = self.formfield_for_dbfield(field, request=request, **kwargs) # type: _FormField # f can be None if the dbfield does not get a FormField (like hidden fields # or auto increment IDs). Only the dbfield has a .name attribute. if f and field.name == "dkimkey": if obj: obj.dkimkey = RSA.generate(2048).exportKey("PEM").decode("utf-8") else: f.initial = RSA.generate(2048).exportKey("PEM").decode("utf-8") return f kwargs["formfield_callback"] = functools.partial(formfield_callback, request=req) form_t = super().get_form(req, obj, **kwargs) return form_t
def process(self, lookup_type, value, connection): """ Returns a tuple of data suitable for inclusion in a WhereNode instance. """ # Because of circular imports, we need to import this here. from django.db.models.base import ObjectDoesNotExist try: if self.field: params = self.field.get_db_prep_lookup(lookup_type, value, connection=connection, prepared=True) db_type = self.field.db_type(connection=connection) else: # This branch is used at times when we add a comparison to NULL # (we don't really want to waste time looking up the associated # field object at the calling location). params = Field().get_db_prep_lookup(lookup_type, value, connection=connection, prepared=True) db_type = None except ObjectDoesNotExist: raise EmptyShortCircuit return (self.alias, self.col, db_type), params
def get_srid(self, geom): """ Returns the default SRID for the given geometry, taking into account the SRID set for the field. For example, if the input geometry has no SRID, then that of the field will be returned. """ gsrid = geom.srid # SRID of given geometry. if gsrid is None or self.srid == -1 or (gsrid == -1 and self.srid != -1): return self.srid else: return gsrid # ### Routines overloaded from Field ###
def num_geom(self, **kwargs): """ Returns the number of geometries if the field is a GeometryCollection or Multi* Field in a `num_geom` attribute on each element of this GeoQuerySet; otherwise the sets with None. """ return self._spatial_attribute('num_geom', {}, **kwargs)
def _prepare(self, field): """ Hook used by Field.get_prep_lookup() to do custom preparation. """ return self
def __init__(self, sql, params, output_field=None): if output_field is None: output_field = fields.Field() self.sql, self.params = sql, params super(RawSQL, self).__init__(output_field=output_field)
def from_db_value(self, value, expression, connection, context): if value: if not isinstance(value, Geometry): value = Geometry(value) srid = value.srid if not srid and self.srid != -1: value.srid = self.srid return value # ### Routines overloaded from Field ###
def get_lookup(self, main_field, for_remote, alias): """ create a fake field for the lookup capability :param CompositeForeignKey main_field: the local fk :param Field for_remote: the remote field to match :return: """
def get_lookup(self, main_field, for_remote, alias): """ create a fake field for the lookup capability :param CompositeForeignKey main_field: the local fk :param Field for_remote: the remote field to match :return: """ lookup_class = for_remote.get_lookup("exact") return lookup_class(for_remote.get_col(alias), self.value)
def _get_model_fields(self, field_names, declared_fields, extra_kwargs): """ Returns all the model fields that are being mapped to by fields on the serializer class. Returned as a dict of 'model field name' -> 'model field'. Used internally by `get_uniqueness_field_options`. """ model = getattr(self.Meta, 'model') model_fields = {} for field_name in field_names: if field_name in declared_fields: # If the field is declared on the serializer field = declared_fields[field_name] source = field.source or field_name else: try: source = extra_kwargs[field_name]['source'] except KeyError: source = field_name if '.' in source or source == '*': # Model fields will always have a simple source mapping, # they can't be nested attribute lookups. continue try: field = model._meta.get_field(source) if isinstance(field, DjangoModelField): model_fields[source] = field except FieldDoesNotExist: pass return model_fields # Determine the validators to apply...