我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用django.db.models.PositiveIntegerField()。
def test_add_check_constraint(self): project_state = self.set_up_test_model() operation = migrations.AlterField( model_name='pony', name='pink', field=models.PositiveIntegerField(default=3) ) new_state = project_state.clone() operation.state_forwards('tests', new_state) self.assertNoConstraint('tests_pony', ['pink'], 'check') with connection.schema_editor() as editor: operation.database_forwards('tests', editor, project_state, new_state) self.assertConstraint('tests_pony', ['pink'], 'check') with connection.schema_editor() as editor: operation.database_backwards('tests', editor, new_state, project_state) self.assertNoConstraint('tests_pony', ['pink'], 'check')
def test_model_clicks_field(self): """Test model 'clicks' field""" field = self.model._meta.get_field('clicks') assert isinstance(field, models.PositiveIntegerField) assert field.verbose_name == 'clicks' assert field.default == 0 assert not field.editable
def test_operation_is_safe(self): assert migration_utils.operation_is_safe(operations.AddField( model_name='unsafemodel', name='safe_field', field=models.IntegerField(null=True), )) assert migration_utils.operation_is_safe(operations.AddField( model_name='unsafemodel', name='field_added', field=models.IntegerField(default=1), preserve_default=False, )) is False assert migration_utils.operation_is_safe(operations.AlterField( model_name='unsafemodel', name='field_added', field=models.PositiveIntegerField(default=10), preserve_default=False )) is False assert migration_utils.operation_is_safe(operations.RenameField( model_name='unsafemodel', old_name='field_added', new_name='field_renamed' )) is False assert migration_utils.operation_is_safe(operations.RemoveField( model_name='unsafemodel', name='field_renamed', )) is False assert migration_utils.operation_is_safe(operations.DeleteModel('unsafemodel')) is False assert migration_utils.operation_is_safe(operations.RunSQL("")) is False assert migration_utils.operation_is_safe(operations.RunPython(lambda: True)) is False
def get_queryset(self): qs = super(AnalysisJobManager, self).get_queryset() qs = (qs.annotate(overall_score=ObjectAtPath('overall_scores', ('overall_score', 'score_normalized'))) .annotate(population_total=ObjectAtPath('overall_scores', ('population_total', 'score_original'), output_field=models.PositiveIntegerField()))) return qs
def test_object_id_field_type_class(self): field = instantiate_object_id_field(models.PositiveIntegerField) self.assertIsInstance(field, models.PositiveIntegerField)
def save(self, *args, **kwargs): # (<django.db.models.fields.PositiveIntegerField>,) is returned if self.repeat not set # I assume i must save() model before default value is given if self.every is None and type(self.repeat) is int and self.repeat is not 1: raise ValidationError('you must set a repeat time (via every=[timedelta]) if you want a function called many times' ' (each time after current time + repeat') super(CallLater, self).save(*args, **kwargs) #used for testing
def test_should_positive_integer_convert_int(): assert_conversion(models.PositiveIntegerField, graphene.Int)
def test_object_id(self): field = ServeLog._meta.get_field('object_id') self.assertModelField(field, models.PositiveIntegerField, False, False)
def test_object_id_field(self): """ The model should have a object_id field """ field = OmniField._meta.get_field('object_id') self.assertIsInstance(field, models.PositiveIntegerField) self.assertFalse(field.blank) self.assertFalse(field.null)
def test_get_concrete_class_for_model_field(self): """ The get_concrete_class_for_model_field method should return the correct OmniField subclass """ self.assertEqual(OmniField.get_concrete_class_for_model_field(models.CharField()), OmniCharField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.NullBooleanField()), OmniBooleanField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.BooleanField()), OmniBooleanField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DateTimeField()), OmniDateTimeField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DecimalField()), OmniDecimalField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.EmailField()), OmniEmailField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.FloatField()), OmniFloatField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.IntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.BigIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.PositiveIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.SmallIntegerField()), OmniIntegerField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.TimeField()), OmniTimeField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.URLField()), OmniUrlField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.SlugField()), OmniSlugField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.FileField()), OmniFileField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.ImageField()), OmniImageField) self.assertEqual(OmniField.get_concrete_class_for_model_field(models.DurationField()), OmniDurationField) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.GenericIPAddressField()), OmniGenericIPAddressField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.CommaSeparatedIntegerField()), OmniCharField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.PositiveSmallIntegerField()), OmniIntegerField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.ForeignKey(DummyModel2)), OmniForeignKeyField ) self.assertEqual( OmniField.get_concrete_class_for_model_field(models.ManyToManyField(DummyModel2)), OmniManyToManyField )
def test_max_length(self): """ The model should have a max_length field """ field = OmniCharField._meta.get_field('max_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertFalse(field.null) self.assertEqual(field.default, 255)
def test_min_length(self): """ The model should have a min_length field """ field = OmniCharField._meta.get_field('min_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertFalse(field.null) self.assertEqual(field.default, 0)
def test_max_length(self): """ The model should have a max_length field """ field = OmniEmailField._meta.get_field('max_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertFalse(field.null) self.assertEqual(field.default, 255)
def test_max_digits(self): """ The model should have a max_digits field """ field = OmniDecimalField._meta.get_field('max_digits') self.assertIsInstance(field, models.PositiveIntegerField) self.assertFalse(field.blank) self.assertFalse(field.null)
def test_decimal_places(self): """ The model should have a decimal_places field """ field = OmniDecimalField._meta.get_field('decimal_places') self.assertIsInstance(field, models.PositiveIntegerField) self.assertFalse(field.blank) self.assertFalse(field.null)
def test_max_length(self): """ The model should have a max_length field """ field = OmniUrlField._meta.get_field('max_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertFalse(field.null) self.assertEqual(field.default, 255)
def test_min_length(self): """ The model should have a min_length field """ field = OmniUrlField._meta.get_field('min_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertFalse(field.null) self.assertEqual(field.default, 0)
def test_max_length(self): """ The model should define a max_length field """ field = OmniFileField._meta.get_field('max_length') self.assertIsInstance(field, models.PositiveIntegerField) self.assertTrue(field.blank) self.assertTrue(field.null)
def get_concrete_class_for_model_field(cls, model_field): """ Method for getting a concrete model class to represent the type of form field required :param model_field: Model Field instance :return: OmniField subclass """ field_mapping = { models.CharField: OmniCharField, models.TextField: OmniCharField, models.BooleanField: OmniBooleanField, models.NullBooleanField: OmniBooleanField, models.DateField: OmniDateField, models.DateTimeField: OmniDateTimeField, models.DecimalField: OmniDecimalField, models.EmailField: OmniEmailField, models.FloatField: OmniFloatField, models.IntegerField: OmniIntegerField, models.BigIntegerField: OmniIntegerField, models.PositiveIntegerField: OmniIntegerField, models.PositiveSmallIntegerField: OmniIntegerField, models.SmallIntegerField: OmniIntegerField, models.CommaSeparatedIntegerField: OmniCharField, models.TimeField: OmniTimeField, models.URLField: OmniUrlField, models.ForeignKey: OmniForeignKeyField, models.ManyToManyField: OmniManyToManyField, models.SlugField: OmniSlugField, models.FileField: OmniFileField, models.ImageField: OmniImageField, models.DurationField: OmniDurationField, models.GenericIPAddressField: OmniGenericIPAddressField } field_mapping.update(cls.get_custom_field_mapping()) return field_mapping.get(model_field.__class__)
def __unicode__(self): if self.name is None or get_language() == 'ja': return self.japanese_name else: return self.name ############################################################ # Action Skill # class ActionSkill(MagiModel): # collection_name = 'action_skill' # # name = models.CharField(_('Action Skill'), max_length=100) # japanese_name = models.CharField(string_concat(_('Action Skill'), ' (', t['Japanese'], ')'), max_length=100) # # def __unicode__(self): # if get_language() == 'ja': # return self.japanese_name # else: # return self.name # # damage = models.CharField(_('Skill Damage'), max_length=200) # combo = models.PositiveIntegerField(_('Skill Combo'), default=13) # effects = models.ManyToManyField('ActionSkillEffect', related_name='skills_with_effect', null=True) ############################################################ # Action Skill Effect # class ActionSkillEffect(MagiModel): # collection_name = 'action_skill_effect' # # i_name = models.CharField(_('Action Skill Effect'), max_length=100) # # bonus_value = models.PositiveIntegerField(_('Effect Value'), null=True) # duration = models.PositiveIntegerField(_('Effect Duration'), null=True) # skill_affinity = models.PositiveIntegerField(_('Skill Affinity'), null=True, choices=SKILL_AFFINITY_CHOICES, # default=IGNORE_AFFINITY) # # def __unicode__(self): # return '{} {}'.format(self.i_name, self.bonus_value) ############################################################ # Weapons