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',
            },
        ),
    ]

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