Help us understand the problem. What is going on with this article?

Django で model のプライマリーキーを設定する

Django で model を作成すると、id が プライマリーキーになります。
テーブルの構造はこのようになります。

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| key        | varchar(10) | NO   |     | NULL    |                |
| name       | varchar(20) | NO   |     | NULL    |                |
| population | int(11)     | NO   |     | NULL    |                |
| date_mod   | date        | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

primary_key=True を設定すると、そのコラムが プライマリーキーになり、 id は生成されなくなります。
次のように設定します。

models.py
# ------------------------------------------------------------------
#
#   models.py
#
#                   Feb/17/2019
# ------------------------------------------------------------------
from django.db import models

class City(models.Model):
    key = models.CharField(max_length=10,primary_key=True)
    name = models.CharField(max_length=20)
    population = models.IntegerField(default=0)
    date_mod = models.DateField()

    def __str__(self):
        return '<City: ' + \
            self.key + ', ' + self.name + '>'
# ------------------------------------------------------------------

これで出来るテーブルは次のようになります。

+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| key        | varchar(10) | NO   | PRI | NULL    |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| population | int(11)     | NO   |     | NULL    |       |
| date_mod   | date        | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

参考
モデルフィールドリファレンス

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away