Python django.db.models.AutoField() Examples
The following are 30
code examples of django.db.models.AutoField().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models
, or try the search function
.
Example #1
Source File: 0002_pk_migration.py From django-rest-passwordreset with BSD 3-Clause "New" or "Revised" License | 7 votes |
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 ), ]
Example #2
Source File: operations.py From python with Apache License 2.0 | 7 votes |
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
Example #3
Source File: test_multidb.py From django-sqlserver with MIT License | 6 votes |
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)
Example #4
Source File: 0002_auto__add_project__add_unique_project_name_organization.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
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'])
Example #5
Source File: introspection.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
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
Example #6
Source File: introspection.py From bioforum with MIT License | 6 votes |
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)
Example #7
Source File: operations.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
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
Example #8
Source File: batch.py From weibo-analysis-system with MIT License | 6 votes |
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')
Example #9
Source File: batch.py From myblog with GNU Affero General Public License v3.0 | 6 votes |
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')
Example #10
Source File: batch.py From CTF_AWD_Platform with MIT License | 6 votes |
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')
Example #11
Source File: batch.py From django_OA with GNU General Public License v3.0 | 6 votes |
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')
Example #12
Source File: introspection.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
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)
Example #13
Source File: introspection.py From openhgsenti with Apache License 2.0 | 6 votes |
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
Example #14
Source File: batch.py From StormOnline with Apache License 2.0 | 6 votes |
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')
Example #15
Source File: batch.py From devops with MIT License | 6 votes |
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')
Example #16
Source File: batch.py From imoocc with GNU General Public License v2.0 | 6 votes |
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')
Example #17
Source File: 0006_auto__add_flowbit.py From scirius with GNU General Public License v3.0 | 6 votes |
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'])
Example #18
Source File: operations.py From python2017 with MIT License | 6 votes |
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
Example #19
Source File: introspection.py From python2017 with MIT License | 6 votes |
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
Example #20
Source File: batch.py From Mxonline3 with Apache License 2.0 | 6 votes |
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')
Example #21
Source File: operations.py From openhgsenti with Apache License 2.0 | 6 votes |
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
Example #22
Source File: introspection.py From python with Apache License 2.0 | 6 votes |
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
Example #23
Source File: models.py From python2017 with MIT License | 5 votes |
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 # Leave defaults for fields that aren't in POST data, except for # checkbox inputs because they don't appear in POST data if not checked. if (f.has_default() and form[f.name].field.widget.value_omitted_from_data(form.data, form.files, form.add_prefix(f.name))): 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 # ModelForms #################################################################
Example #24
Source File: 0002_pk_migration.py From django-rest-passwordreset with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_migrations_for_django_before_21(): return [ # add a new id field (without primary key information) migrations.AddField( model_name='resetpasswordtoken', name='id', field=models.IntegerField(null=True), preserve_default=True, ), # fill the new pk field migrations.RunPython( populate_auto_incrementing_pk_field, migrations.RunPython.noop ), # add primary key information to id field migrations.AlterField( model_name='resetpasswordtoken', name='id', field=models.AutoField(primary_key=True, serialize=False) ), # remove primary key information from 'key' field migrations.AlterField( model_name='resetpasswordtoken', name='key', field=models.CharField(db_index=True, max_length=64, unique=True, verbose_name='Key'), ), ]
Example #25
Source File: models.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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)
Example #26
Source File: test_converter.py From graphene-django with MIT License | 5 votes |
def test_should_auto_convert_id(): assert_conversion(models.AutoField, graphene.ID, primary_key=True)
Example #27
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
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
Example #28
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def convert_values(self, value, field): """ Coerce the value returned by the database backend into a consistent type that is compatible with the field type. """ if value is None: return value internal_type = field.get_internal_type() if internal_type == 'FloatField': return float(value) elif (internal_type and (internal_type.endswith('IntegerField') or internal_type == 'AutoField')): return int(value) return value
Example #29
Source File: 0001_initial.py From django-rest-authemail with GNU General Public License v3.0 | 5 votes |
def forwards(self, orm): # Adding model 'MyUser' db.create_table(u'accounts_myuser', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('password', self.gf('django.db.models.fields.CharField')(max_length=128)), ('last_login', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)), ('is_superuser', self.gf('django.db.models.fields.BooleanField')(default=False)), ('first_name', self.gf('django.db.models.fields.CharField')(max_length=30, blank=True)), ('last_name', self.gf('django.db.models.fields.CharField')(max_length=30, blank=True)), ('email', self.gf('django.db.models.fields.EmailField')(unique=True, max_length=255)), ('is_staff', self.gf('django.db.models.fields.BooleanField')(default=False)), ('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)), ('date_joined', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)), ('is_verified', self.gf('django.db.models.fields.BooleanField')(default=False)), ('date_of_birth', self.gf('django.db.models.fields.DateField')(null=True, blank=True)), )) db.send_create_signal(u'accounts', ['MyUser']) # Adding M2M table for field groups on 'MyUser' m2m_table_name = db.shorten_name(u'accounts_myuser_groups') db.create_table(m2m_table_name, ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('myuser', models.ForeignKey(orm[u'accounts.myuser'], null=False)), ('group', models.ForeignKey(orm[u'auth.group'], null=False)) )) db.create_unique(m2m_table_name, ['myuser_id', 'group_id']) # Adding M2M table for field user_permissions on 'MyUser' m2m_table_name = db.shorten_name(u'accounts_myuser_user_permissions') db.create_table(m2m_table_name, ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('myuser', models.ForeignKey(orm[u'accounts.myuser'], null=False)), ('permission', models.ForeignKey(orm[u'auth.permission'], null=False)) )) db.create_unique(m2m_table_name, ['myuser_id', 'permission_id'])
Example #30
Source File: 0008_auto_add_user_star_field_to_project.py From kobo-predict with BSD 2-Clause "Simplified" License | 5 votes |
def forwards(self, orm): # Adding M2M table for field user_stars on 'Project' m2m_table_name = db.shorten_name(u'api_project_user_stars') db.create_table(m2m_table_name, ( ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), ('project', models.ForeignKey(orm['api.project'], null=False)), ('user', models.ForeignKey(orm[u'auth.user'], null=False)) )) db.create_unique(m2m_table_name, ['project_id', 'user_id'])