中创建自动递增整数字段

中创建自动递增整数字段

本文介绍了如何在 Django 中创建自动递增整数字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为购物车制作一个 Order 模型,我需要制作一个在下订单时自动递增的字段:

class Order(models.Model):购物车 = 模型.ForeignKey(购物车)add_date = models.DateTimeField(auto_now_add=True)order_number = models.IntegerField()enable = models.BooleanField(default=True)

如何使 IntegerField 自动递增?

解决方案

在 Django

1:我们有名称为id"的默认字段;这是自动增量.
2 : 您可以使用 AutoField 定义自动增量字段领域.

类订单(模型.模型):auto_increment_id = models.AutoField(primary_key=True)#you use primary_key = True 如果你不想使用 django 给你的模型的默认字段id"

数据库设计

+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+|表 |创建表 |+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+|core_order |创建表`core_order`(`auto_increment_id` int(11) NOT NULL AUTO_INCREMENT,主键(`auto_increment_id`)) 引擎=InnoDB 默认字符集=latin1 |+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+1 行(0.01 秒)

如果你想使用django的默认id作为增量字段.

类订单(模型.模型):dd_date = models.DateTimeField(auto_now_add=True)

数据库设计

+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+|表 |创建表 |+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+|core_order |创建表`core_order`(`id` int(11) NOT NULL AUTO_INCREMENT,`dd_date` 日期时间非空,主键(`id`)) 引擎=InnoDB 默认字符集=latin1 |+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+

I am making an Order model for a shopping cart and I need to make a field that auto increments when the order is made:

class Order(models.Model):
    cart = models.ForeignKey(Cart)
    add_date = models.DateTimeField(auto_now_add=True)
    order_number = models.IntegerField()
    enable = models.BooleanField(default=True)

How do I make the IntegerField auto increment?

解决方案

In Django

1 : we have default field with name "id" which is auto increment.
2 : You can define a auto increment field using AutoFieldfield.

class Order(models.Model):
    auto_increment_id = models.AutoField(primary_key=True)
    #you use primary_key = True if you do not want to use default field "id" given by django to your model

db design

+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                  |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| core_order | CREATE TABLE `core_order` (
  `auto_increment_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`auto_increment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

If you want to use django's default id as increment field .

class Order(models.Model):
    dd_date = models.DateTimeField(auto_now_add=True)

db design

+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                    |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| core_order | CREATE TABLE `core_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dd_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+

这篇关于如何在 Django 中创建自动递增整数字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:28