Here are the examples of the python api django.db.models.Model taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
145 Examples
3
View Complete Implementation : test_deprecated_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_IPAddressField_deprecated(self):
clast IPAddressModel(models.Model):
ip = models.IPAddressField()
model = IPAddressModel()
self.astertEqual(
model.check(),
[checks.Error(
'IPAddressField has been removed except for support in '
'historical migrations.',
hint='Use GenericIPAddressField instead.',
obj=IPAddressModel._meta.get_field('ip'),
id='fields.E900',
)],
)
3
View Complete Implementation : test_checks.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pointing_to_missing_model(self):
clast Model(models.Model):
rel = GenericRelation('MissingModel')
self.astertEqual(Model.rel.field.check(), [
checks.Error(
"Field defines a relation with model 'MissingModel', "
"which is either not installed, or is abstract.",
obj=Model.rel.field,
id='fields.E300',
)
])
3
View Complete Implementation : test_ordinary_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_valid_field(self):
clast Model(models.Model):
field = models.CharField(
max_length=255,
choices=[
('1', 'item1'),
('2', 'item2'),
],
db_index=True,
)
field = Model._meta.get_field('field')
self.astertEqual(field.check(), [])
3
View Complete Implementation : test_models.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_non_valid(self):
clast RelationModel(models.Model):
past
clast Model(models.Model):
relation = models.ManyToManyField(RelationModel)
clast Meta:
ordering = ['relation']
self.astertEqual(Model.check(), [
Error(
"'ordering' refers to the nonexistent field 'relation'.",
obj=Model,
id='models.E015',
),
])
3
View Complete Implementation : test_checks.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_content_type_field_pointing_to_wrong_model(self):
clast 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.astertEqual(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',
)
])
3
View Complete Implementation : test_models.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_m2m_to_concrete_and_proxy_allowed(self):
clast A(models.Model):
past
clast Through(models.Model):
a = models.ForeignKey('A', models.CASCADE)
c = models.ForeignKey('C', models.CASCADE)
clast ThroughProxy(Through):
clast Meta:
proxy = True
clast C(models.Model):
mm_a = models.ManyToManyField(A, through=Through)
mm_aproxy = models.ManyToManyField(A, through=ThroughProxy, related_name='proxied_m2m')
self.astertEqual(C.check(), [])
3
View Complete Implementation : test_models.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_db_column_clash(self):
clast Model(models.Model):
foo = models.IntegerField()
bar = models.IntegerField(db_column='foo')
self.astertEqual(Model.check(), [
Error(
"Field 'bar' has column name 'foo' that is used by "
"another field.",
hint="Specify a 'db_column' for the field.",
obj=Model,
id='models.E007',
)
])
3
View Complete Implementation : test_ordinary_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_non_iterable_choices(self):
clast Model(models.Model):
field = models.CharField(max_length=10, choices='bad')
field = Model._meta.get_field('field')
self.astertEqual(field.check(), [
Error(
"'choices' must be an iterable (e.g., a list or tuple).",
obj=field,
id='fields.E004',
),
])
3
View Complete Implementation : test_ordinary_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bad_max_length_value(self):
clast Model(models.Model):
field = models.CharField(max_length="bad")
field = Model._meta.get_field('field')
self.astertEqual(field.check(), [
Error(
"'max_length' must be a positive integer.",
obj=field,
id='fields.E121',
),
])
3
View Complete Implementation : test_ordinary_fields.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_missing_max_length(self):
clast Model(models.Model):
field = models.CharField()
field = Model._meta.get_field('field')
self.astertEqual(field.check(), [
Error(
"CharFields must define a 'max_length' attribute.",
obj=field,
id='fields.E120',
),
])