本文整理汇总了Python中django.db.models.PositiveIntegerField方法的典型用法代码示例。如果您正苦于以下问题:Python models.PositiveIntegerField方法的具体用法?Python models.PositiveIntegerField怎么用?Python models.PositiveIntegerField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.PositiveIntegerField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: document_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def document_field(field):
"""
The default ``field_factory`` method for converting Django field instances to ``elasticsearch_dsl.Field`` instances.
Auto-created fields (primary keys, for example) and one-to-many fields (reverse FK relationships) are skipped.
"""
if field.auto_created or field.one_to_many:
return None
if field.many_to_many:
return RawMultiString
defaults = {
models.DateField: dsl.Date(),
models.DateTimeField: dsl.Date(),
models.IntegerField: dsl.Long(),
models.PositiveIntegerField: dsl.Long(),
models.BooleanField: dsl.Boolean(),
models.NullBooleanField: dsl.Boolean(),
models.SlugField: dsl.String(index='not_analyzed'),
models.DecimalField: dsl.Double(),
models.FloatField: dsl.Float(),
}
return defaults.get(field.__class__, RawString)
示例2: test_invalid_content_type_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_invalid_content_type_field(self):
class Model(models.Model):
content_type = models.IntegerField() # should be ForeignKey
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
self.assertEqual(Model.content_object.check(), [
checks.Error(
"'Model.content_type' is not a ForeignKey.",
hint=(
"GenericForeignKeys must use a ForeignKey to "
"'contenttypes.ContentType' as the 'content_type' field."
),
obj=Model.content_object,
id='contenttypes.E003',
)
])
示例3: test_content_type_field_pointing_to_wrong_model
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_content_type_field_pointing_to_wrong_model(self):
class Model(models.Model):
content_type = models.ForeignKey('self', models.CASCADE) # should point to ContentType
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
self.assertEqual(Model.content_object.check(), [
checks.Error(
"'Model.content_type' is not a ForeignKey to 'contenttypes.ContentType'.",
hint=(
"GenericForeignKeys must use a ForeignKey to "
"'contenttypes.ContentType' as the 'content_type' field."
),
obj=Model.content_object,
id='contenttypes.E004',
)
])
示例4: test_missing_generic_foreign_key
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_missing_generic_foreign_key(self):
class TaggedItem(models.Model):
content_type = models.ForeignKey(ContentType, models.CASCADE)
object_id = models.PositiveIntegerField()
class Bookmark(models.Model):
tags = GenericRelation('TaggedItem')
self.assertEqual(Bookmark.tags.field.check(), [
checks.Error(
"The GenericRelation defines a relation with the model "
"'contenttypes_tests.TaggedItem', but that model does not have a "
"GenericForeignKey.",
obj=Bookmark.tags.field,
id='contenttypes.E004',
)
])
示例5: test_to_fields_not_checked_if_related_model_doesnt_exist
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_to_fields_not_checked_if_related_model_doesnt_exist(self):
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
parent = ForeignObject(
'invalid_models_tests.Parent',
on_delete=models.SET_NULL,
from_fields=('a', 'b'),
to_fields=('a', 'b'),
)
field = Child._meta.get_field('parent')
self.assertEqual(field.check(), [
Error(
"Field defines a relation with model 'invalid_models_tests.Parent', "
"which is either not installed, or is abstract.",
id='fields.E300',
obj=field,
),
])
示例6: test_pointing_to_swapped_model
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_pointing_to_swapped_model(self):
class Replacement(models.Model):
pass
class SwappedModel(models.Model):
content_type = models.ForeignKey(ContentType, models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
class Meta:
swappable = 'TEST_SWAPPED_MODEL'
class Model(models.Model):
rel = GenericRelation('SwappedModel')
self.assertEqual(Model.rel.field.check(), [
checks.Error(
"Field defines a relation with the model "
"'contenttypes_tests.SwappedModel', "
"which has been swapped out.",
hint="Update the relation to point at 'settings.TEST_SWAPPED_MODEL'.",
obj=Model.rel.field,
id='fields.E301',
)
])
示例7: test_field_name_ending_with_underscore
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_field_name_ending_with_underscore(self):
class TaggedItem(models.Model):
content_type = models.ForeignKey(ContentType, models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
class InvalidBookmark(models.Model):
tags_ = GenericRelation('TaggedItem')
self.assertEqual(InvalidBookmark.tags_.field.check(), [
checks.Error(
'Field names must not end with an underscore.',
obj=InvalidBookmark.tags_.field,
id='fields.E001',
)
])
示例8: test_check_subset_composite_foreign_object
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_check_subset_composite_foreign_object(self):
class Parent(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
class Meta:
unique_together = (('a', 'b'),)
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
d = models.CharField(max_length=255)
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b', 'c'),
to_fields=('a', 'b', 'c'),
related_name='children',
)
self.assertEqual(Child._meta.get_field('parent').check(from_model=Child), [])
示例9: test_max_length_warning
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_max_length_warning(self):
class Model(models.Model):
integer = models.IntegerField(max_length=2)
biginteger = models.BigIntegerField(max_length=2)
smallinteger = models.SmallIntegerField(max_length=2)
positiveinteger = models.PositiveIntegerField(max_length=2)
positivesmallinteger = models.PositiveSmallIntegerField(max_length=2)
for field in Model._meta.get_fields():
if field.auto_created:
continue
with self.subTest(name=field.name):
self.assertEqual(field.check(), [
DjangoWarning(
"'max_length' is ignored when used with %s." % field.__class__.__name__,
hint="Remove 'max_length' from field",
obj=field,
id='fields.W122',
)
])
示例10: __str__
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def __str__(self):
return self.name
# Create your models here.
#from django.contrib.contenttypes.models import ContentType
#from django.contrib.contenttypes import generic
# A solution for foreignkey
# class Foo(models.Model):
#company_type = models.ForeignKey(ContentType)
#company_id = models.PositiveIntegerField()
#company = generic.GenericForeignKey('company_type', 'company_id')
#seller = Seller.objects.create()
#buyer = Buyer.objects.create()
#foo1 = Foo.objects.create(company = seller)
#foo2 = Foo.objects.create(company = buyer)
# foo1.company
# <Seller: Seller object>
# foo2.company
# <Buyer: Buyer object>
# https://stackoverflow.com/questions/30551057/django-what-are-the-alternatives-to-having-a-foreignkey-to-an-abstract-class
# https://lukeplant.me.uk/blog/posts/avoid-django-genericforeignkey/
示例11: test_alter_field_add_constraint_check__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_alter_field_add_constraint_check__ok():
with cmp_schema_editor() as editor:
old_field = models.IntegerField()
old_field.set_attributes_from_name('field')
new_field = models.PositiveIntegerField()
new_field.set_attributes_from_name('field')
editor.alter_field(Model, old_field, new_field)
assert editor.collected_sql == timeouts(
'ALTER TABLE "tests_model" ADD CONSTRAINT "tests_model_field_0a53d95f_check" '
'CHECK ("field" >= 0) NOT VALID;',
) + [
'ALTER TABLE "tests_model" VALIDATE CONSTRAINT "tests_model_field_0a53d95f_check";',
]
assert editor.django_sql == [
'ALTER TABLE "tests_model" ADD CONSTRAINT "tests_model_field_0a53d95f_check" CHECK ("field" >= 0);',
]
示例12: test_alter_field_add_constraint_check__with_flexible_timeout__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_alter_field_add_constraint_check__with_flexible_timeout__ok():
with cmp_schema_editor() as editor:
old_field = models.IntegerField()
old_field.set_attributes_from_name('field')
new_field = models.PositiveIntegerField()
new_field.set_attributes_from_name('field')
editor.alter_field(Model, old_field, new_field)
assert editor.collected_sql == timeouts(
'ALTER TABLE "tests_model" ADD CONSTRAINT "tests_model_field_0a53d95f_check" '
'CHECK ("field" >= 0) NOT VALID;',
) + flexible_statement_timeout(
'ALTER TABLE "tests_model" VALIDATE CONSTRAINT "tests_model_field_0a53d95f_check";',
)
assert editor.django_sql == [
'ALTER TABLE "tests_model" ADD CONSTRAINT "tests_model_field_0a53d95f_check" CHECK ("field" >= 0);',
]
示例13: test_alter_field_drop_constraint_check__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_alter_field_drop_constraint_check__ok(mocker):
mocker.patch.object(connection, 'cursor')
mocker.patch.object(connection.introspection, 'get_constraints').return_value = {
'tests_model_field_0a53d95f_check': {
'columns': ['field'],
'primary_key': False,
'unique': False,
'foreign_key': None,
'check': True,
'index': False,
'definition': None,
'options': None,
}
}
with cmp_schema_editor() as editor:
old_field = models.PositiveIntegerField()
old_field.set_attributes_from_name('field')
new_field = models.IntegerField()
new_field.set_attributes_from_name('field')
editor.alter_field(Model, old_field, new_field)
assert editor.collected_sql == timeouts(editor.django_sql)
assert editor.django_sql == [
'ALTER TABLE "tests_model" DROP CONSTRAINT "tests_model_field_0a53d95f_check";',
]
示例14: test_object_id_field_type_class
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_object_id_field_type_class(self):
field = instantiate_object_id_field(models.PositiveIntegerField)
self.assertIsInstance(field, models.PositiveIntegerField)
示例15: test_should_positive_integer_convert_int
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import PositiveIntegerField [as 别名]
def test_should_positive_integer_convert_int():
assert_conversion(models.PositiveIntegerField, graphene.Int)