我试图对某个字段进行自动递增,但不知道执行此操作的正确方法。我正在使用postgres来管理我的数据库:我尝试了Autofield数据类型,但是没有用。我想django使用的是默认ID字段,我想用我的WO_ID字段替换它。
models.py
class Workorder(models.Model):
WO_ID = models.BigIntegerField(blank=True, primary_key=True)
WO_DateDefWO = models.DateField(default=datetime.now)
WO_DateSched = models.DateField(blank=True, null=True)
WO_DateFinished = models.DateField(blank=True, null=True)
WO_ST_ID_Sign = models.BigIntegerField(blank=True, null=True)
WO_Status_ID = models.BigIntegerField(blank=True, null=True, default=1, choices=STATUS_CHOICES)
WO_Type_ID = models.BigIntegerField(blank=True, null=True, default=1, choices=TYPE_CHOICES)
WO_Comments = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_ID = models.BigIntegerField(blank=True, null=True)
WO_Nav_Kons_ID = models.CharField(max_length=12, blank=True, null=True)
WO_Nav_Name = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_CustAdr = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_Debt = models.FloatField(blank=True, null=True)
WO_Nav_PropCode = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_DepCode = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_PhoneNo = models.CharField(max_length=254, blank=True, null=True)
WO_Nav_ReasonCompl = models.CharField(max_length=254, blank=True, null=True)
WO_NightShift = models.BooleanField(default=False)
WO_Priority = models.BigIntegerField(blank=True, null=True)
WO_RE_ID = models.BigIntegerField(blank=True, null=True)
WO_MapUrl = models.CharField(max_length=254, blank=True, null=True)
def __unicode__(self):
return self.WO_ID
views.py
from django.shortcuts import render, get_object_or_404, redirect
from django import forms
from django.views.generic import TemplateView
from django.core.serializers import serialize
from django.http import HttpResponse
from MMS.models import Workorder
from MMS import forms
from MMS.forms import CreateWorkorder
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from . import forms
def workorders_list(request):
workorders_all = Workorder.objects.all().values_list('WO_ID', flat=True)
return render(request, "workorders_list.html")
def home_page(request):
return render(request, "home_page.html")
def workorder_create(request):
if request.method == "POST":
form = CreateWorkorder(request.POST)
if form.is_valid():
CreateWorkorder_item = form.save(commit=False)
CreateWorkorder_item.save()
return redirect('/workorder/' + str(CreateWorkorder_item.WO_ID) + '/')
else:
form = CreateWorkorder()
return render(request, 'workorders/workorder_create.html', {'form': form})
def workorder_edit(request, id=None):
item = get_object_or_404(Workorder, WO_ID=id)
form = CreateWorkorder(request.POST or None, instance=item)
if form.is_valid():
form.save()
return redirect('/workorder/' + str(item.WO_ID) + '/')
return render(request, 'workorders/workorder_edit.html', {'form': form})
def workorder(request, id):
workorder = Workorder.objects.get(WO_ID=id)
return render(request, 'workorders/workorder.html', {'workorder': workorder})
PostgreSQL自动增量代码:
CREATE SEQUENCE public.MMS_workorder_id_seq
INCREMENT 1
START 1
alter table MMS_workorder ALTER COLUMN "WO_ID" SET DEFAULT nextval('MMS_workorder_id_seq');
答案 0 :(得分:1)
默认情况下,Django为每个模型提供一个名为id
的自动递增字段。参见https://docs.djangoproject.com/en/2.1/topics/db/models/#automatic-primary-key-fields
如果要创建额外的自动递增字段,可以使用AutoField
https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.AutoField
创建自定义ID字段
class MyModel(models.Model):
wo_id = models.AutoField(primary_key=True)
这会跳过默认id
字段的创建,并创建名称为wo_id
的自动递增主字段。