Админ не регистрирует тип адреса как получилось (я использую django)? - PullRequest
0 голосов
/ 05 октября 2019

Это проблема:

если адрес отправлен, адрес должен автоматически зарегистрировать ТИП АДРЕСА в разделе адресов моего администратора, как вы можете видеть на этом рисунке, где адрес представлен в форме проверки (тип адреса должен быть адресом доставки): введите описание изображения здесь

это код моей модели.py:

from django.db import models

from billing.models import BillingProfile

ADDRESS_TYPES = (
    ('billing', 'Billing'),
    ('shipping', 'Shipping'),
)
# Create your models here.
class Address(models.Model):
    billing_profile = models.ForeignKey(BillingProfile)
    address_type    = models.CharField(max_length=120, choices=ADDRESS_TYPES)
    address_line_1  = models.CharField(max_length=120)
    address_line_2  = models.CharField(max_length=120, null=True, blank=True)
    city            = models.CharField(max_length=120)
    country         = models.CharField(max_length=120)
    state           = models.CharField(max_length=120)
    postal_code     = models.CharField(max_length=120)

    def __str__(self):
        return str(self.billing_profile)

и это виды.py:

from django.shortcuts import render, redirect
from django.utils.http import is_safe_url
# CRUD create update retrieve delete

from billing.models import BillingProfile
from .forms import AddressForm

def  checkout_address_create_view(request):
    form = AddressForm(request.POST or None)
    context = {
        "form": form
    }
    next_ = request.GET.get('next')
    next_post = request.POST.get('next')
    redirect_path = next_ or next_post or None
    if form.is_valid():
        instance = form.save(commit=False)
        billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request)
        if billing_profile is not None:
            instance.billing_profile = billing_profile
            instance.address_type = request.POST.get('address_type', 'shipping')
            instance.save()
        else:
            print("Error here")
            return redirect("cart:checkout")

        if is_safe_url(redirect_path, request.get_host()):
            return redirect(redirect_path)
        else:
            return redirect("cart:checkout")
    return redirect("cart:checkout")

Это правило должно сработать в views.py, но, к сожалению, не отображается в админке (как видно на картинке :(:

instance.address_type = request.POST.get('address_type', 'shipping')

Кто-тоесть совет?

...