本文整理汇总了Python中django.db.models.AutoField方法的典型用法代码示例。如果您正苦于以下问题:Python models.AutoField方法的具体用法?Python models.AutoField怎么用?Python models.AutoField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.AutoField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: change_models
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def change_models(self, queryset, cleaned_data):
n = queryset.count()
data = {}
fields = self.opts.fields + self.opts.many_to_many
for f in fields:
if not f.editable or isinstance(f, models.AutoField) \
or not f.name in cleaned_data:
continue
data[f] = cleaned_data[f.name]
if n:
for obj in queryset:
for f, v in data.items():
f.save_form_data(obj, v)
obj.save()
self.message_user(_("Successfully change %(count)d %(items)s.") % {
"count": n, "items": model_ngettext(self.opts, n)
}, 'success')
示例2: sequence_list
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def sequence_list(self):
"Returns a list of information about all DB sequences for all models in all apps."
from django.apps import apps
from django.db import models, router
sequence_list = []
for app_config in apps.get_app_configs():
for model in router.get_migratable_models(app_config, self.connection.alias):
if not model._meta.managed:
continue
if model._meta.swapped:
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
示例3: sequence_reset_sql
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def sequence_reset_sql(self, style, model_list):
from django.db import models
output = []
query = self._sequence_reset_sql
for model in model_list:
for f in model._meta.local_fields:
if isinstance(f, models.AutoField):
table_name = self.quote_name(model._meta.db_table)
sequence_name = self._get_sequence_name(model._meta.db_table)
column_name = self.quote_name(f.column)
output.append(query % {'sequence': sequence_name,
'table': table_name,
'column': column_name})
# Only one AutoField is allowed per model, so don't
# continue to loop
break
for f in model._meta.many_to_many:
if not f.rel.through:
table_name = self.quote_name(f.m2m_db_table())
sequence_name = self._get_sequence_name(f.m2m_db_table())
column_name = self.quote_name('id')
output.append(query % {'sequence': sequence_name,
'table': table_name,
'column': column_name})
return output
示例4: forwards
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def forwards(self, orm):
# Adding model 'Project'
db.create_table('api_project', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length=255)),
('organization', self.gf('django.db.models.fields.related.ForeignKey')(related_name='project_organization', to=orm['auth.User'])),
('created_by', self.gf('django.db.models.fields.related.ForeignKey')(related_name='project_creator', to=orm['auth.User'])),
('date_created', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
('date_modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
))
db.send_create_signal('api', ['Project'])
# Adding unique constraint on 'Project', fields ['name', 'organization']
db.create_unique('api_project', ['name', 'organization_id'])
# Adding M2M table for field projects on 'Team'
db.create_table('api_team_projects', (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('team', models.ForeignKey(orm['api.team'], null=False)),
('project', models.ForeignKey(orm['api.project'], null=False))
))
db.create_unique('api_team_projects', ['team_id', 'project_id'])
开发者ID:awemulya,项目名称:kobo-predict,代码行数:24,代码来源:0002_auto__add_project__add_unique_project_name_organization.py
示例5: get_field_type
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def get_field_type(self, data_type, description):
if data_type == cx_Oracle.NUMBER:
precision, scale = description[4:6]
if scale == 0:
if precision > 11:
return 'BigAutoField' if description.is_autofield else 'BigIntegerField'
elif precision == 1:
return 'BooleanField'
elif description.is_autofield:
return 'AutoField'
else:
return 'IntegerField'
elif scale == -127:
return 'FloatField'
return super().get_field_type(data_type, description)
示例6: get_migrations_for_django_21_and_newer
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def get_migrations_for_django_21_and_newer():
return [
# remove primary key information from 'key' field
migrations.AlterField(
model_name='resetpasswordtoken',
name='key',
field=models.CharField(db_index=True, primary_key=False, max_length=64, unique=True, verbose_name='Key'),
),
# add a new id field
migrations.AddField(
model_name='resetpasswordtoken',
name='id',
field=models.AutoField(primary_key=True, serialize=False),
preserve_default=False,
),
migrations.RunPython(
populate_auto_incrementing_pk_field,
migrations.RunPython.noop
),
]
示例7: sequence_list
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def sequence_list(self):
"Returns a list of information about all DB sequences for all models in all apps."
from django.apps import apps
from django.db import models, router
sequence_list = []
for app_config in apps.get_app_configs():
for model in router.get_migratable_models(app_config, self.connection.alias):
if not model._meta.managed:
continue
if model._meta.swapped:
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.remote_field.through is None:
sequence_list.append({'table': f.m2m_db_table(), 'column': None})
return sequence_list
示例8: sequence_reset_sql
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def sequence_reset_sql(self, style, model_list):
from django.db import models
output = []
query = self._sequence_reset_sql
for model in model_list:
for f in model._meta.local_fields:
if isinstance(f, models.AutoField):
table_name = self.quote_name(model._meta.db_table)
sequence_name = self._get_sequence_name(model._meta.db_table)
column_name = self.quote_name(f.column)
output.append(query % {'sequence': sequence_name,
'table': table_name,
'column': column_name})
# Only one AutoField is allowed per model, so don't
# continue to loop
break
for f in model._meta.many_to_many:
if not f.remote_field.through:
table_name = self.quote_name(f.m2m_db_table())
sequence_name = self._get_sequence_name(f.m2m_db_table())
column_name = self.quote_name('id')
output.append(query % {'sequence': sequence_name,
'table': table_name,
'column': column_name})
return output
示例9: forwards
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def forwards(self, orm):
# Adding model 'Flowbit'
db.create_table(u'rules_flowbit', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
('set', self.gf('django.db.models.fields.BooleanField')(default=False)),
('isset', self.gf('django.db.models.fields.BooleanField')(default=False)),
('enable', self.gf('django.db.models.fields.BooleanField')(default=True)),
('source', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['rules.Source'])),
))
db.send_create_signal(u'rules', ['Flowbit'])
# Adding M2M table for field flowbits on 'Rule'
m2m_table_name = db.shorten_name(u'rules_rule_flowbits')
db.create_table(m2m_table_name, (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('rule', models.ForeignKey(orm[u'rules.rule'], null=False)),
('flowbit', models.ForeignKey(orm[u'rules.flowbit'], null=False))
))
db.create_unique(m2m_table_name, ['rule_id', 'flowbit_id'])
示例10: change_models
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def change_models(self, queryset, cleaned_data):
n = queryset.count()
data = {}
for f in self.opts.fields:
if not f.editable or isinstance(f, models.AutoField) \
or not f.name in cleaned_data:
continue
data[f] = cleaned_data[f.name]
if n:
for obj in queryset:
for f, v in data.items():
f.save_form_data(obj, v)
obj.save()
self.message_user(_("Successfully change %(count)d %(items)s.") % {
"count": n, "items": model_ngettext(self.opts, n)
}, 'success')
示例11: _test_create_model
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def _test_create_model(self, app_label, should_run):
"""
CreateModel honors multi-db settings.
"""
operation = migrations.CreateModel(
"Pony",
[("id", models.AutoField(primary_key=True))],
)
# Test the state alteration
project_state = ProjectState()
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
# Test the database alteration
self.assertTableNotExists("%s_pony" % app_label)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
if should_run:
self.assertTableExists("%s_pony" % app_label)
else:
self.assertTableNotExists("%s_pony" % app_label)
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards(app_label, editor, new_state, project_state)
self.assertTableNotExists("%s_pony" % app_label)
示例12: forwards
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def forwards(self, orm):
# Adding model 'Organization'
db.create_table(u'crowdataapp_organization', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length='128')),
('description', self.gf('django.db.models.fields.TextField')()),
))
db.send_create_signal(u'crowdataapp', ['Organization'])
# Adding M2M table for field users on 'Organization'
m2m_table_name = db.shorten_name(u'crowdataapp_organization_users')
db.create_table(m2m_table_name, (
('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
('organization', models.ForeignKey(orm[u'crowdataapp.organization'], null=False)),
('user', models.ForeignKey(orm[u'auth.user'], null=False))
))
db.create_unique(m2m_table_name, ['organization_id', 'user_id'])
# Adding field 'DocumentSetFormEntry.organization'
db.add_column(u'crowdataapp_documentsetformentry', 'organization',
self.gf('django.db.models.fields.related.ForeignKey')(to=orm['crowdataapp.Organization'], null=True, blank=True),
keep_default=False)
# Adding field 'UserProfile.current_organization'
db.add_column(u'crowdataapp_userprofile', 'current_organization',
self.gf('django.db.models.fields.related.ForeignKey')(to=orm['crowdataapp.Organization'], null=True, blank=True),
keep_default=False)
开发者ID:crowdata,项目名称:crowdata,代码行数:29,代码来源:0028_auto__add_organization__add_field_documentsetformentry_organization__a.py
示例13: _done
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def _done(self):
cleaned_data = self.get_all_cleaned_data()
exclude = self.admin_view.exclude
opts = self.admin_view.opts
instance = self.admin_view.org_obj or self.admin_view.model()
file_field_list = []
for f in opts.fields:
if not f.editable or isinstance(f, models.AutoField) \
or not f.name in cleaned_data:
continue
if exclude and f.name in exclude:
continue
# Defer saving file-type fields until after the other fields, so a
# callable upload_to can use the values from other fields.
if isinstance(f, models.FileField):
file_field_list.append(f)
else:
f.save_form_data(instance, cleaned_data[f.name])
for f in file_field_list:
f.save_form_data(instance, cleaned_data[f.name])
instance.save()
for f in opts.many_to_many:
if f.name in cleaned_data:
f.save_form_data(instance, cleaned_data[f.name])
self.admin_view.new_obj = instance
示例14: construct_instance
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def construct_instance(form, instance, fields=None, exclude=None):
"""
Constructs and returns a model instance from the bound ``form``'s
``cleaned_data``, but does not save the returned instance to the
database.
"""
from django.db import models
opts = instance._meta
cleaned_data = form.cleaned_data
file_field_list = []
for f in opts.fields:
if not f.editable or isinstance(f, models.AutoField) \
or f.name not in cleaned_data:
continue
if fields is not None and f.name not in fields:
continue
if exclude and f.name in exclude:
continue
# Defer saving file-type fields until after the other fields, so a
# callable upload_to can use the values from other fields.
if isinstance(f, models.FileField):
file_field_list.append(f)
else:
f.save_form_data(instance, cleaned_data[f.name])
for f in file_field_list:
f.save_form_data(instance, cleaned_data[f.name])
return instance
示例15: add_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import AutoField [as 别名]
def add_fields(self, form, index):
"""Add a hidden field for the object's primary key."""
from django.db.models import AutoField, OneToOneField, ForeignKey
self._pk_field = pk = self.model._meta.pk
# If a pk isn't editable, then it won't be on the form, so we need to
# add it here so we can tell which object is which when we get the
# data back. Generally, pk.editable should be false, but for some
# reason, auto_created pk fields and AutoField's editable attribute is
# True, so check for that as well.
def pk_is_not_editable(pk):
return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk)))
if pk_is_not_editable(pk) or pk.name not in form.fields:
if form.is_bound:
# If we're adding the related instance, ignore its primary key
# as it could be an auto-generated default which isn't actually
# in the database.
pk_value = None if form.instance._state.adding else form.instance.pk
else:
try:
if index is not None:
pk_value = self.get_queryset()[index].pk
else:
pk_value = None
except IndexError:
pk_value = None
if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey):
qs = pk.rel.to._default_manager.get_queryset()
else:
qs = self.model._default_manager.get_queryset()
qs = qs.using(form.instance._state.db)
if form._meta.widgets:
widget = form._meta.widgets.get(self._pk_field.name, HiddenInput)
else:
widget = HiddenInput
form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=widget)
super(BaseModelFormSet, self).add_fields(form, index)