django.db.models AutoField Example Code

AutoField is a Django ORM mapping from your Python code to an integer-type column in your relational database.

The Django project has great documentation for AutoField as well as all of the other column fields.

Note that AutoField is defined within the django.db.models.fields module but is typically referenced from django.db.models rather than including the fields module reference.

Example 1 from django-axes

django-axes (project documentation and PyPI package information is a code library for Django projects to track failed login attempts against a web application. The goal of the project is to make it easier for you to stop people and scripts from hacking your Django-powered website.

The code for django-axes is open source under the MIT license and maintained by the group of developers known as Jazzband.

django-axes / axes / migrations / 0001_initial.py

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='AccessAttempt',
            fields=[
                ('id', 
                 models.AutoField(verbose_name='ID', 
                                  serialize=False, 
                                  auto_created=True, 
                                  primary_key=True)),
                ('user_agent', models.CharField(max_length=255)),
                ('ip_address', 
                 models.GenericIPAddressField(null=True, 
                                              verbose_name='IP Address')),
                ('username', models.CharField(max_length=255, 
                                              null=True)),
                ('trusted', models.BooleanField(default=False)),
                ('http_accept', 
                 models.CharField(max_length=1025, 
                                  verbose_name='HTTP Accept')),
                ('path_info', 
                 models.CharField(max_length=255, 
                                  verbose_name='Path')),
                ('attempt_time', 
                 models.DateTimeField(auto_now_add=True)),
                ('get_data', 
                 models.TextField(verbose_name='GET Data')),
                ('post_data', models.TextField(verbose_name='POST Data')),
                ('failures_since_start', 
                  models.PositiveIntegerField(verbose_name='Failed Logins')),
            ],
            options={
                'ordering': ['-attempt_time'],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='AccessLog',
            fields=[
                ('id', models.AutoField(verbose_name='ID', 
                                        serialize=False, 
                                        auto_created=True, 
                                        primary_key=True)),
                ('user_agent', models.CharField(max_length=255)),
                ('ip_address', 
                 models.GenericIPAddressField(null=True, 
                                              verbose_name='IP Address')),
                ('username', models.CharField(max_length=255, null=True)),
                ('trusted', models.BooleanField(default=False)),
                ('http_accept', 
                 models.CharField(max_length=1025, 
                                  verbose_name='HTTP Accept')),
                ('path_info', 
                 models.CharField(max_length=255, 
                                  verbose_name='Path')),
                ('attempt_time', 
                 models.DateTimeField(auto_now_add=True)),
                ('logout_time', models.DateTimeField(null=True, 
                                                     blank=True)),
            ],
            options={
                'ordering': ['-attempt_time'],
                'abstract': False,
            },
        ),
    ]

Example 2 from django-filer

django-filer (project documentation) is a file management library for uploading and organizing files and images in Django's admin interface. The project's code is available under the BSD 3-Clause "New" or "Revised" open source license.

django-filer / filer / migrations / 0003_thumbnailoption.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('filer', '0002_auto_20150606_2003'),
    ]

    operations = [
        migrations.CreateModel(
            name='ThumbnailOption',
            fields=[
                ('id', models.AutoField(verbose_name='ID', 
                                          serialize=False, 
                                          auto_created=True, 
                                          primary_key=True)),
                ('name', models.CharField(max_length=100, 
                                          verbose_name='name')),
                ('width', 
                 models.IntegerField(help_text='width in pixel.', 
                                     verbose_name='width')),
                ('height', 
                 models.IntegerField(help_text='height in pixel.', 
                                     verbose_name='height')),
                ('crop', 
                 models.BooleanField(default=True, 
                                     verbose_name='crop')),
                ('upscale', 
                 models.BooleanField(default=True, 
                                     verbose_name='upscale')),
            ],
            options={
                'ordering': ('width', 'height'),
                'verbose_name': 'thumbnail option',
                'verbose_name_plural': 'thumbnail options',
            },
        ),
    ]
TwilioQuest logo.

Learn more Python in the awesome free TwilioQuest 16-bit adventure game.

1. Introduction 2. Development Environments 3. Data 4. Web Development 5. Deployment 6. DevOps Changelog What Full Stack Means About the Author Future Directions Page Statuses Django Code Examples django.urls.path django.conf.urls.url django.contrib.admin django.db.models Model django.dispatch Signal django.db.models.signal django.core.mail.send_mail django.core.mail.messages EmailMessage django.conf settings django.forms django.contrib.admin.sites register django.core.management.base BaseCommand django.utils.timezone django.contrib.auth get_user_model django.contrib.auth.decorators login_required django.core.exceptions ImproperlyConfigured django.utils.html format_html django.contrib.auth.hashers make_password django.http Http404 django.http HttpResponse django.http HttpResponseForbidden django.http HttpResponseBadRequest django.contrib.admin.filters SimpleListFilter django.urls.exceptions NoReverseMatch django.http HttpResponsePermanentRedirect django.http HttpResponseNotModified django.http HttpResponseRedirect django.apps.config AppConfig django.db OperationalError django.forms CharField flask_sqlalchemy.SQLAlchemy Model django.db.models CharField django.db.models TextField django.db.models BooleanField flask request flask redirect django.db.models DateTimeField django.db.models IntegerField django.db.models AutoField django.db.models DateField django.db.models FileField django.db.models SlugField django.template.response TemplateResponse django.template.response SimpleTemplateResponse SQLAlchemy Model Examples django.urls reverse_lazy Flask Core and Extensions Code Examples ...or view the full table of contents.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2019