当数量多于一个时,分配序列号

时间:2016-08-05 12:00:25

标签: python django

我有一个模型,其中生成一个唯一的序列号,并且有一个数量字段,用户可以在其中选择他们想要制作的部分。把它想象成购物订单。我希望如果用户选择多个数量,则为每个单独的部分分配序列号。 机型:

from django.shortcuts import render, get_object_or_404, redirect, get_object_or_404
from django.contrib import messages
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import RequestContext, loader
from django.contrib.auth.decorators import login_required
from .forms import PartRequestForm
from .models import PartRequest
from tande.models import Project, Person

 # Create your views here.
@login_required
def home(request):
    latest_project_list = Project.objects.all()
    context = {'latest_project_list': latest_project_list}
    return render(request, 'buildpage/home.html', context)

def partrequestinfo(request):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    req_form = PartRequestForm(request.POST or None, request.FILES or None)
    if req_form.is_valid():
            instance = req_form.save(commit=False)
            instance.user = request.user
            print request.POST
            print req_form.cleaned_data.get("requester")
            print instance
            instance.save()
            messages.success(request, "Successfully Created")
            return HttpResponseRedirect(instance.get_absolute_url())
    else: 
        context = {
                "req_form": req_form
        }
    return render(request, "buildpage/partrequestinfo.html", context)

def partrequestdetail(request, slug=None):
    print "in partrequestdetail function"
    instance = get_object_or_404(PartRequest, slug=slug)
    context = {
        "project_id": instance.project_id,
        "instance": instance,
    }
    return render(request, 'buildpage/partrequestdetail.html', context)

def partrequestupdate(request, slug=None):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    instance = get_object_or_404(PartRequest, slug=slug)
    req_form = PartRequestForm(request.POST or None, request.FILES or None, instance=instance)
    if req_form.is_valid():
            instance = req_form.save(commit=False)
            print request.POST
            print req_form.cleaned_data.get("requester")
            print instance
            instance.save()
            messages.success(request, "Saved")
            return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "project_id": instance.project_id,
        "instance": instance,
        "req_form": req_form,
    }
    return render(request, 'buildpage/partrequestinfo.html', context)

def partrequestdelete(request, slug=None):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    instance = get_object_or_404(PartRequest, slug=slug)
    messages.success(request, "Successfully Deleted")
    return redirect("buildpage:home")

def preparebuildlist(request, slug=None):

    return render(request, 'buildpage/preparebuildlist.html')

观点:

<inherits name="org.gwtbootstrap3.extras.select.Select"/>

1 个答案:

答案 0 :(得分:0)

我无法真正理解你的模特,但我认为你需要的东西(如果你还没有它)是Part模型。 Part模型中的一个字段是部件的序列号,可以根据需要自动生成,因此当用户请求X个部件时,您只需创建并保存X Part对象。