我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.db.models.fields.DecimalField()。
def from_dict(self, dict_payload): for field in self._meta.fields: if field.name not in dict_payload: continue if isinstance(field, EmbeddedModelField): self.from_dict_nested(field.name, field.embedded_model, dict_payload) continue if isinstance(field, ListField): if isinstance(field.item_field, EmbeddedModelField): self.from_dict_nested_list(field.name, field.item_field.embedded_model, dict_payload) continue if isinstance(field, DateTimeField): self._date_from_string(field.name, dict_payload.get(field.name)) elif isinstance(field, DecimalField): self._decimal_from_string(field.name, dict_payload.get(field.name)) elif field.name in dict_payload: setattr(self, field.name, dict_payload[field.name]) if 'id' in dict_payload.keys(): self.id = dict_payload['id']
def get_django_field_map(self): from django.db.models import fields as djf return [ (djf.AutoField, PrimaryKeyField), (djf.BigIntegerField, BigIntegerField), # (djf.BinaryField, BlobField), (djf.BooleanField, BooleanField), (djf.CharField, CharField), (djf.DateTimeField, DateTimeField), # Extends DateField. (djf.DateField, DateField), (djf.DecimalField, DecimalField), (djf.FilePathField, CharField), (djf.FloatField, FloatField), (djf.IntegerField, IntegerField), (djf.NullBooleanField, partial(BooleanField, null=True)), (djf.TextField, TextField), (djf.TimeField, TimeField), (djf.related.ForeignKey, ForeignKeyField), ]
def set_decimal(instance, field, max_digits=10): """ DecimalField """ max_digits = min(max_digits, field.max_digits) min_value = -10 ** max_digits max_value = 10 ** max_digits for validator in field.validators: if isinstance(validator, MinValueValidator): min_value = max(min_value, validator.limit_value) if isinstance(validator, MaxValueValidator): max_value = min(max_value, validator.limit_value) value = random.randint(min_value, max_value) value = Decimal(value) / 10 ** field.decimal_places setattr(instance, field.name, value)
def _resolve_output_field(self): source_field = self.get_source_fields()[0] if isinstance(source_field, (IntegerField, DecimalField)): self._output_field = FloatField() super(Avg, self)._resolve_output_field()