Here are the examples of the python api django.db.models taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
5 Examples
3
View Complete Implementation : djpeewee.py
Copyright MIT License
Author : danielecook
Copyright MIT License
Author : danielecook
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),
]
0
View Complete Implementation : djpeewee.py
Copyright MIT License
Author : danielecook
Copyright MIT License
Author : danielecook
def _translate_model(self,
model,
mapping,
max_depth=None,
backrefs=False,
exclude=None):
if exclude and model in exclude:
return
if max_depth is None:
max_depth = -1
from django.db.models import fields as djf
options = model._meta
if mapping.get(options.object_name):
return
mapping[options.object_name] = None
attrs = {}
# Sort fields such that nullable fields appear last.
field_key = lambda field: (field.null and 1 or 0, field)
for model_field in sorted(options.fields, key=field_key):
# Get peewee equivalent for this field type.
converted = self.convert_field(model_field)
# Special-case ForeignKey fields.
if converted is ForeignKeyField:
if max_depth != 0:
related_model = model_field.rel.to
model_name = related_model._meta.object_name
# If we haven't processed the related model yet, do so now.
if model_name not in mapping:
mapping[model_name] = None # Avoid endless recursion.
self._translate_model(
related_model,
mapping,
max_depth=max_depth - 1,
backrefs=backrefs,
exclude=exclude)
if mapping[model_name] is None:
# Cycle detected, put an integer field here.
logger.warn('Cycle detected: %s: %s',
model_field.name, model_name)
attrs[model_field.name] = IntegerField(
db_column=model_field.column)
else:
related_name = (model_field.rel.related_name or
model_field.related_query_name())
if related_name.endswith('+'):
related_name = '__%s:%s:%s' % (
options,
model_field.name,
related_name.strip('+'))
attrs[model_field.name] = ForeignKeyField(
mapping[model_name],
related_name=related_name,
db_column=model_field.column,
)
else:
attrs[model_field.name] = IntegerField(
db_column=model_field.column)
elif converted:
attrs[model_field.name] = converted()
klast = type(options.object_name, (Model,), attrs)
klast._meta.db_table = options.db_table
klast._meta.database.interpolation = '%s'
mapping[options.object_name] = klast
if backrefs:
# Follow back-references for foreign keys.
for rel_obj in options.get_all_related_objects():
if rel_obj.model._meta.object_name in mapping:
continue
self._translate_model(
rel_obj.model,
mapping,
max_depth=max_depth - 1,
backrefs=backrefs,
exclude=exclude)
# Load up many-to-many relationships.
for many_to_many in options.many_to_many:
if not isinstance(many_to_many, djf.related.ManyToManyField):
continue
self._translate_model(
many_to_many.rel.through,
mapping,
max_depth=max_depth, # Do not decrement.
backrefs=backrefs,
exclude=exclude)
0
View Complete Implementation : module_finder_test.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dw
Copyright BSD 3-Clause "New" or "Revised" License
Author : dw
def test_django_db_models(self):
import django.db.models
related = self.call('django.db.models')
self.maxDiff=None
self.astertEquals(related, [
u'django',
u'django.core.exceptions',
u'django.db',
u'django.db.models',
u'django.db.models.aggregates',
u'django.db.models.base',
u'django.db.models.deletion',
u'django.db.models.expressions',
u'django.db.models.fields',
u'django.db.models.fields.files',
u'django.db.models.fields.related',
u'django.db.models.fields.subclasting',
u'django.db.models.loading',
u'django.db.models.manager',
u'django.db.models.query',
u'django.db.models.signals',
])
0
View Complete Implementation : module_finder_test.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dw
Copyright BSD 3-Clause "New" or "Revised" License
Author : dw
@unittest2.skipIf(
condition=(sys.version_info >= (3, 0)),
reason='broken due to ancient vendored six.py'
)
def test_django_db_models(self):
import django.db.models
related = self.call('django.db.models')
self.astertEquals(related, [
u'django',
u'django.conf',
u'django.conf.global_settings',
u'django.core',
u'django.core.exceptions',
u'django.core.files',
u'django.core.files.base',
u'django.core.files.images',
u'django.core.files.locks',
u'django.core.files.move',
u'django.core.files.storage',
u'django.core.files.utils',
u'django.core.signals',
u'django.core.validators',
u'django.db',
u'django.db.backends',
u'django.db.backends.signals',
u'django.db.backends.util',
u'django.db.models.aggregates',
u'django.db.models.base',
u'django.db.models.constants',
u'django.db.models.deletion',
u'django.db.models.expressions',
u'django.db.models.fields',
u'django.db.models.fields.files',
u'django.db.models.fields.proxy',
u'django.db.models.fields.related',
u'django.db.models.fields.subclasting',
u'django.db.models.loading',
u'django.db.models.manager',
u'django.db.models.options',
u'django.db.models.query',
u'django.db.models.query_utils',
u'django.db.models.related',
u'django.db.models.signals',
u'django.db.models.sql',
u'django.db.models.sql.aggregates',
u'django.db.models.sql.constants',
u'django.db.models.sql.datastructures',
u'django.db.models.sql.expressions',
u'django.db.models.sql.query',
u'django.db.models.sql.subqueries',
u'django.db.models.sql.where',
u'django.db.transaction',
u'django.db.utils',
u'django.dispatch',
u'django.dispatch.dispatcher',
u'django.dispatch.saferef',
u'django.forms',
u'django.forms.fields',
u'django.forms.forms',
u'django.forms.formsets',
u'django.forms.models',
u'django.forms.util',
u'django.forms.widgets',
u'django.utils',
u'django.utils._os',
u'django.utils.crypto',
u'django.utils.datastructures',
u'django.utils.dateformat',
u'django.utils.dateparse',
u'django.utils.dates',
u'django.utils.datetime_safe',
u'django.utils.decorators',
u'django.utils.deprecation',
u'django.utils.encoding',
u'django.utils.formats',
u'django.utils.functional',
u'django.utils.html',
u'django.utils.html_parser',
u'django.utils.importlib',
u'django.utils.ipv6',
u'django.utils.itercompat',
u'django.utils.module_loading',
u'django.utils.numberformat',
u'django.utils.safestring',
u'django.utils.six',
u'django.utils.text',
u'django.utils.timezone',
u'django.utils.translation',
u'django.utils.tree',
u'django.utils.tzinfo',
u'pytz',
u'pytz.exceptions',
u'pytz.lazy',
u'pytz.tzfile',
u'pytz.tzinfo',
])
0
View Complete Implementation : dumpscript.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def get_models(app_labels):
""" Gets a list of models for the given app labels, with some exceptions.
TODO: If a required model is referenced, it should also be included.
Or at least discovered with a get_or_create() call.
"""
from django.db.models import get_app, get_apps, get_model
from django.db.models import get_models as get_all_models
# These models are not to be output, e.g. because they can be generated automatically
# TODO: This should be "appname.modelname" string
EXCLUDED_MODELS = (ContentType, )
models = []
# If no app labels are given, return all
if not app_labels:
for app in get_apps():
models += [m for m in get_all_models(app) if m not in EXCLUDED_MODELS]
return models
# Get all relevant apps
for app_label in app_labels:
# If a specific model is mentioned, get only that model
if "." in app_label:
app_label, model_name = app_label.split(".", 1)
models.append(get_model(app_label, model_name))
# Get all models for a given app
else:
models += [m for m in get_all_models(get_app(app_label)) if m not in EXCLUDED_MODELS]
return models