本文整理匯總了Python中django.db.models.get_models方法的典型用法代碼示例。如果您正苦於以下問題:Python models.get_models方法的具體用法?Python models.get_models怎麽用?Python models.get_models使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models
的用法示例。
在下文中一共展示了models.get_models方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: handle
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def handle(self, *args, **options):
if not args:
apps = []
for model in get_models():
apps.append(get_app(model._meta.app_label))
else:
apps = []
for arg in args:
apps.append(get_app(arg))
for app in apps:
create_permissions(app, get_models(), int(options.get('verbosity', 0)))
示例2: django_table_names
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def django_table_names(self, only_existing=False):
"""
Returns a list of all table names that have associated Django models and
are in INSTALLED_APPS.
If only_existing is True, the resulting list will only include the tables
that actually exist in the database.
"""
from django.db import models, router
tables = set()
for app in models.get_apps():
for model in models.get_models(app):
if not model._meta.managed:
continue
if not router.allow_syncdb(self.connection.alias, model):
continue
tables.add(model._meta.db_table)
tables.update([f.m2m_db_table() for f in model._meta.local_many_to_many])
tables = list(tables)
if only_existing:
existing_tables = self.table_names()
tables = [
t
for t in tables
if self.table_name_converter(t) in existing_tables
]
return tables
示例3: installed_models
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def installed_models(self, tables):
"Returns a set of all models represented by the provided list of table names."
from django.db import models, router
all_models = []
for app in models.get_apps():
for model in models.get_models(app):
if router.allow_syncdb(self.connection.alias, model):
all_models.append(model)
tables = list(map(self.table_name_converter, tables))
return set([
m for m in all_models
if self.table_name_converter(m._meta.db_table) in tables
])
示例4: sequence_list
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def sequence_list(self):
"Returns a list of information about all DB sequences for all models in all apps."
from django.db import models, router
apps = models.get_apps()
sequence_list = []
for app in apps:
for model in models.get_models(app):
if not model._meta.managed:
continue
if model._meta.swapped:
continue
if not router.allow_syncdb(self.connection.alias, model):
continue
for f in model._meta.local_fields:
if isinstance(f, models.AutoField):
sequence_list.append({'table': model._meta.db_table, 'column': f.column})
break # Only one AutoField is allowed per model, so don't bother continuing.
for f in model._meta.local_many_to_many:
# If this is an m2m using an intermediate table,
# we don't need to reset the sequence.
if f.rel.through is None:
sequence_list.append({'table': f.m2m_db_table(), 'column': None})
return sequence_list
示例5: handle_app
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def handle_app(self, app, **options):
connection = connections[options.get('database')]
return '\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app, include_auto_created=True)))
示例6: sql_custom
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def sql_custom(app, style, connection):
"Returns a list of the custom table modifying SQL statements for the given app."
output = []
app_models = get_models(app)
for model in app_models:
output.extend(custom_sql_for_model(model, style, connection))
return output
示例7: sql_indexes
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def sql_indexes(app, style, connection):
"Returns a list of the CREATE INDEX SQL statements for all models in the given app."
output = []
for model in models.get_models(app):
output.extend(connection.creation.sql_indexes_for_model(model, style))
return output
示例8: get_app_models
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def get_app_models(app_name, include_auto_created=False):
if VERSION >= (1, 7):
return apps.get_app_config(app_name).get_models(
include_auto_created=include_auto_created)
else:
return get_models(get_app(app_name),
include_auto_created=include_auto_created)
示例9: get_notification_models
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def get_notification_models():
"""Utility that gets all notification models"""
return [model for model in models.get_models()
if getattr(model, 'create_notification', False) is True]
示例10: update_permissions_after_migration
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def update_permissions_after_migration(app,**kwargs):
"""
Update app permission just after every migration.
This is based on app django_extensions update_permissions management command.
"""
from django.db.models import get_app, get_models
from django.contrib.auth.management import create_permissions
create_permissions(get_app(app), get_models(), 2 if settings.DEBUG else 0)
示例11: sql_create
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import get_models [as 別名]
def sql_create(app, style, connection):
"Returns a list of the CREATE TABLE SQL statements for the given app."
if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy':
# This must be the "dummy" database backend, which means the user
# hasn't set ENGINE for the database.
raise CommandError("Django doesn't know which syntax to use for your SQL statements,\n" +
"because you haven't properly specified the ENGINE setting for the database.\n" +
"see: https://docs.djangoproject.com/en/dev/ref/settings/#databases")
# Get installed models, so we generate REFERENCES right.
# We trim models from the current app so that the sqlreset command does not
# generate invalid SQL (leaving models out of known_models is harmless, so
# we can be conservative).
app_models = models.get_models(app, include_auto_created=True)
final_output = []
tables = connection.introspection.table_names()
known_models = set([model for model in connection.introspection.installed_models(tables) if model not in app_models])
pending_references = {}
for model in app_models:
output, references = connection.creation.sql_create_model(model, style, known_models)
final_output.extend(output)
for refto, refs in references.items():
pending_references.setdefault(refto, []).extend(refs)
if refto in known_models:
final_output.extend(connection.creation.sql_for_pending_references(refto, style, pending_references))
final_output.extend(connection.creation.sql_for_pending_references(model, style, pending_references))
# Keep track of the fact that we've created the table for this model.
known_models.add(model)
# Handle references to tables that are from other apps
# but don't exist physically.
not_installed_models = set(pending_references.keys())
if not_installed_models:
alter_sql = []
for model in not_installed_models:
alter_sql.extend(['-- ' + sql for sql in
connection.creation.sql_for_pending_references(model, style, pending_references)])
if alter_sql:
final_output.append('-- The following references should be added but depend on non-existent tables:')
final_output.extend(alter_sql)
return final_output