Django:根据选择自动填充数据

cpjpxq1n  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(94)

我正在创建一个发票/PO表单,需要一些帮助。我用了4个模型:客户端、发货、计费和PO模型。我有一对一的关系之间的航运和结算模型的客户端模型和两个作为外键(请让我知道,如果这种方法仍然是好的)在PO模型。
models.py ClientBillingAddress和ShippingAddress完全相同,只是“billing”替换为“shipping”

class ClientBillingAddress(models.Model):
    client = models.OneToOneField(Client, on_delete=models.CASCADE, related_name='billing_address')
    billing_location_name = models.CharField(max_length=255, unique=True)
    address_line_1 = models.CharField(max_length=200, blank=True, null=True)
    address_line_2 = models.CharField(max_length=100, blank=True, null=True)
    address_city = models.CharField(max_length=50, blank=True, null=True)
    address_province = models.CharField(max_length=50, blank=True, null=True)
    address_postal = models.CharField(max_length=15, blank=True, null=True)
    address_country = models.CharField(max_length=50, blank=True, null=True)
    phone = models.CharField(max_length=15, null=True, blank=True)
    email_to  = models.CharField(max_length=150, null=True, blank=True)
    email_cc = models.CharField(max_length=150, null=True, blank=True)
    bill_contact_name = models.CharField(null=True, blank=True, max_length=50)
    is_default = models.BooleanField(default=False)

    def get_formatted_address(self):
        address_lines = [self.billing_location_name, self.address_line_1, self.address_line_2]
        address_city_info = f"{self.address_city}, {self.address_province}, {self.address_postal}"
        address_lines.append(address_city_info)
        address_lines.append(self.address_country)

        return "\n".join(address_lines)

    def __str__(self):
        return self.billing_location_name

class ClientPO(models.Model):
    client_PO_number = models.CharField(max_length=50, unique=True)
    customer_company = models.ForeignKey(Client, on_delete=models.CASCADE, related_name="customer_name_PO")
    order_reserved_total = models.PositiveBigIntegerField(default=0)
    order_total = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=Decimal("0.00"))

    order_line = models.PositiveBigIntegerField(default=0)
    order_product = models.ForeignKey(InventoryItemDetail, on_delete=models.CASCADE, related_name="order_products")
    order_qty = models.PositiveBigIntegerField(default=0)

    order_unit_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=Decimal("0.00"))
    order_extended_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=Decimal("0.00"))
    order_units = models.CharField(max_length=10, choices=UNIT_TYPES, default='EA')

    order_ship_to = models.ForeignKey(ClientShippingAddress, on_delete=models.CASCADE, related_name="order_shipto")
    order_bill_to = models.ForeignKey(ClientBillingAddress, on_delete=models.CASCADE, related_name="order_billto")
    order_open_qty = models.PositiveBigIntegerField(default=0)
    order_shipped_qty = models.PositiveBigIntegerField(default=0)

    order_status = models.CharField(max_length=2, choices=ORDER_STATUS_CHOICES, default='PN')
    order_delivery_due = models.DateTimeField(auto_now=False)

    date_created = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

    def calculate_extended_value(self):
        # Calculate total value based on the item cost and quantity
        order_extended_price = self.order_unit_price * self.order_qty
        return order_extended_price

    def __str__(self):
        return self.client_PO_number

字符串
网址:forms.py

class ClientPOForm(forms.ModelForm):
    billing_address = forms.CharField(
        widget=forms.Textarea(attrs={
            'class': 'form-textarea w-full resize-none text-xs rounded-lg border border-slate-300 bg-transparent p-2.5 placeholder:text-slate-400/70 hover:border-slate-400 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent',
            'readonly': 'readonly',
        })
    )

    shipping_address = forms.CharField(
        widget=forms.Textarea(attrs={
            'class': 'form-textarea w-full resize-none text-xs rounded-lg border border-slate-300 bg-transparent p-2.5 placeholder:text-slate-400/70 hover:border-slate-400 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent',
            'readonly': 'readonly',
        })
    )

    class Meta:
        model = ClientPO
        fields = [
            'client_PO_number', 'order_product','order_qty','order_unit_price','order_units',
            'order_ship_to','order_bill_to','order_delivery_due','customer_company'
        ]


views.py

def customer_add_purchase_order(request):
    if request.method == 'POST':
        customer_po_form = ClientPOForm(request.POST)

        if customer_po_form.is_valid():
            customer_po = customer_po_form.save(commit=False)
            customer_po.customer_company = customer_po_form.cleaned_data['customer_company']
            customer_po.save()

            customer_po.slug = slugify(customer_po.client_PO_number)
            customer_po.save()

            messages.success(request, "Customer PO added successfully.")
            return redirect('customer_orders')
        else:
            print(customer_po_form.errors)
    else:
        customer_po_form = ClientPOForm()

    context = {
        'customer_po_form': customer_po_form,
    }

    return render(request, 'client/add_po.html', context)


我不知道从这里如何处理它。从本质上讲,“customer_company”有一个.pk,我想在选择公司时捕获该pk并将其填充到模板中的textfield中。
在这里

<textarea
id="customer_bill_to_txt"
rows="5"
class="form-textarea"> 
{{ customer_po_form.order_bill_to }} </textarea>


我曾尝试从forms.py获取账单和运输信息
但这仅填充textarea内的类信息,而不是来自客户端计费/发货的信息。

class ClientPOForm(forms.ModelForm):
:
:
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Set the initial values for billing_address and shipping_address fields
        if self.instance.pk:
            billing_address = self.instance.order_bill_to.get_formatted_billing_address()
            shipping_address = self.instance.order_ship_to.get_formatted_shipping_address()
            self.initial['billing_address'] = billing_address
            self.initial['shipping_address'] = shipping_address


我也试过 AJAX 从视图和内部模板。

views.py
def get_client_addresses(request):
    if request.method == 'POST' and request.is_ajax():
        client_id = request.POST.get('client_id')
        client = get_object_or_404(Client, pk=client_id)
        billing_address = get_object_or_404(ClientBillingAddress, client=client)
        shipping_address = get_object_or_404(ClientShippingAddress, client=client)

        data = {
            'billing_address': billing_address.get_formatted_address(),
            'shipping_address': shipping_address.get_formatted_address(),
        }

        return JsonResponse(data)
    else:
        return JsonResponse({'error': 'Invalid request'})

template.html

$(document).ready(function() {
  // Add an event listener to the customer select field
  $('#id_customer_company').change(function() {
    // Get the selected customer ID
    var customerId = $(this).val();

    // Make an AJAX request to get the billing and shipping addresses for the selected customer
    $.ajax({
      url: `/get_client_addresses/${customerId}/`,
      type: 'GET',
      dataType: 'json',
      success: function(data) {
        // Update the textareas with the retrieved addresses
        $('#customer_bill_to_txt').val(data.billing_address);
        $('#customer_ship_to_txt').val(data.shipping_address);
      },
      error: function(xhr, status, error) {
        console.log(xhr.responseText);
      }
    });
  });
});


我很确定我遗漏了一些东西,但到目前为止,当选择company_name时,我无法在表单中填充bill_To和ship_to。如果有人能帮忙,我将不胜感激。

ctehm74n

ctehm74n1#

您 AJAX 中使用了type: 'GET',,但在

if request.method == 'POST' and request.is_ajax():

字符串
您的视图需要type: 'POST',(如果使用jquery 1.9+,则为method: 'POST',

相关问题