如何解决用户和模型问题?

时间:2019-10-31 20:55:13

标签: python django django-models

我正在创建一个项目,但有2个问题。

在models.py中,我有:

from django.db import models
from children.models import Child
from django.contrib.auth.models import User

class OrderLineItem(models.Model):
    order= models.ForeignKey("Order", null=False, on_delete=models.CASCADE, related_name="order")
    child = models.ForeignKey(Child, null=False, on_delete=models.CASCADE)
    donation = models.IntegerField(blank=False)

    def __str__(self):
        return "{0}-{1}".format(
            self.donation, self.child.name)
class Order(models.Model):
    full_name = models.ForeignKey(User, blank=False, on_delete=models.CASCADE)
    phone_number = models.CharField(max_length=20, blank=False)
    country = models.CharField(max_length=40, blank=False)
    postcode = models.CharField(max_length=20, blank=True)
    town_or_city = models.CharField(max_length=40, blank=False)
    street_address1 = models.CharField(max_length=40, blank=False)
    street_address2 = models.CharField(max_length=40, blank=True)
    date = models.DateField()
    orderlineitem = models.ForeignKey(OrderLineItem, on_delete=models.CASCADE, related_name="orderlineitem")

    def __str__(self):
        return "{0}-{1}-{2}".format(self.id, self.date, self.full_name)

在结帐html中,我有:


<form role="form" method="post" id="payment-form" action="{% url 'checkout' %}">
    <legend>Payment Details</legend>

    <div id="credit-card-errors" style="display: none;">
        <div id="alert-message block-message error" id="stripe-error-message"></div>
    </div>

    <div class="form-group col-md-6">
        {{ order_form | as_bootstrap }}
    </div>

    <div class="form-group col-md-6">
        {{ payment_form | as_bootstrap }}
    </div>

    {% csrf_token %}
    <div class="form-group col-md-12">
        <input class=" btn btn-primary" id="submit_payment_btn" name="commit" type="submit" value="Submit Payment">
    </div>
</form>

有2个问题: 1.我无法在full_name中显示当前用户,但是所有用户 2.它只允许每个用户下一个订单,这意味着如果我尝试用同一用户下另一个订单,则会出现此错误:具有此全名的订单已存在。 我该如何解决?

0 个答案:

没有答案