如何在提交时将Selectbox中的选择添加到数据库中

时间:2019-06-15 12:42:49

标签: python django drop-down-menu

我有一个要提交的表格,所有数据都保存到“订单”模型中。字段之一是html selectbox,因此当我提交表单时,它不会保存到模型中。我想弄清楚如何使下拉选择项像其他数据一样保存。

Forms.py

class CreateOrderForm(forms.ModelForm):
    class Meta:
        model = Orders
        fields = ('reference', 'ultimate_consignee', 'ship_to')

ADD_ORDER.HTML

  <div class="column_order">
        <label for="form.reference">Reference ID: </label>
        {{ form.reference }}
        <br>
        <label> Ultimate Consignee:
         <br>
        <select class="select-css">
          {% for element in objectlist %}
            <option value="{{ element.c_name }}">{{ element.c_name }}</option>
          {% endfor %}
        </select>
        </label>
        <br>
        <label for="form.ship_to">Ship To: </label>
        <br>
        {{ form.ship_to }}
  </div>

Views.py(以说明我如何填充上面的选择框)

def add_order(request):
    if request.method == "POST":
        form = CreateOrderForm(request.POST)
        objectlist = Customers.objects.all()


        if form.is_valid():
            reference_id = form.cleaned_data.get('reference')
            form.save()
            return redirect('add_manifest')
    else:
    form = CreateOrderForm()
    objectlist = Customers.objects.all()
    context = {
        'form': form,
        'objectlist': objectlist,
    }

    return render(request, 'add_order.html', context)

编辑 Models.py

class Customers(models.Model):

    ultimate_consignee = models.CharField(max_length=100)
    c_address = models.CharField(max_length=1000)
    c_country = models.CharField(max_length=50)

def _str_(self):
    return self.ultimate_consignee

class Orders(models.Model):

    reference = models.CharField(max_length=50, unique=True)
    ultimate_consignee = models.ForeignKey(Customers, blank=True)
    ship_to = models.CharField(max_length=500, blank=True)
    vessel = models.CharField(max_length=100, blank=True)
    booking_no = models.CharField(max_length=50, blank=True)
    POL = models.CharField(max_length=50, blank=True)
    DOL = models.DateField(blank=True)
    COO = models.CharField(max_length=50, blank=True)
    POE = models.CharField(max_length=50, blank=True)
    ETA = models.DateField(blank=True)
    pickup_no = models.CharField(max_length=50, blank=True)
    terms = models.CharField(max_length=1000, blank=True)
    sales_contact = models.CharField(max_length=100, blank=True)
    trucking_co = models.CharField(max_length=100, blank=True)
    loading_loc = models.CharField(max_length=100, blank=True)
    inspector = models.CharField(max_length=50, blank=True)
    total_cases = models.IntegerField(blank=True)
    total_fob = models.DecimalField(max_digits=10, decimal_places=2, blank=True)
    freight_forwarder = models.CharField(max_length=100, blank=True)
    commodity = models.CharField(max_length=200, blank=True)
    is_airshipment = models.BooleanField(default=False)
    credit = models.DecimalField(max_digits=10, decimal_places=2, blank=True)

def _str_(self):
    return self.reference

如您所见,我在.html文件中创建的选择框实际上与表单无关。因此,选择中的任何数据都不会保存到Orders模型中,这就是我希望在此处进行的操作。有什么建议吗?

0 个答案:

没有答案