簡體   English   中英

Django 表單與依賴的 ForeignKey forms 在一頁

[英]Django Form with dependent ForeignKey forms on one Page

美好的一天,我正在嘗試在一頁上使用依賴外鍵 forms 實現 django。 我有三個穆德爾商店、產品和產品服裝。 它們都分別與 ForeignKey 相關。 用戶可以單獨創建他們的商店,並被重定向到他們現在必須上傳產品的商店詳細信息視圖。 但這一次,我希望他們同時上傳產品和產品服裝。 下面是我目前在商店詳細視圖中的 models.py 和 views.py。 我得到的錯誤也是:

error.py
NOT NULL constraint failed: product_productclothing.product_id
models.py
class Store(models.Model):
    owner = models.ForeignKey(Profile, null=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=100, unique=True)
    slug = AutoSlugField(populate_from='name', unique=True)
    description = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.name

class Product(models.Model):
    store = models.ForeignKey(Store, null=True, on_delete=models.SET_NULL)
    owner = models.ForeignKey(Profile, null=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=9, decimal_places=2, verbose_name=_("Regular price"))

class ProductClothing(models.Model):
    CLOTHING_GENDER_CHOICES = (
        ('M', 'Male',),
        ('F', 'Female',),
        ('U', 'Unisex',),
    )
    CLOTHING_TYPE_CHOICES = (
        ('dress', 'Dresses',),
        ('fabric', 'Fabrics',),
        ('shirt', 'Shirts',),
        ('suit', 'Suits',),
        ('tshirt', 'T-Shirts',),
        ('base_layers', 'Base_Layers',),
        ('blazer', 'Blazers',),
    )

    product = models.OneToOneField(Product, on_delete=models.CASCADE)
    gender = models.CharField(max_length=10, choices=CLOTHING_GENDER_CHOICES, blank=True, null=True)
    clothing_type = models.CharField(max_length=255, choices=CLOTHING_TYPE_CHOICES, blank=True, null=True)

    def __str__(self):
        return self.product.title

views.py
@login_required
def store_dashboard_view(request, slug):
    store = get_object_or_404(Store, slug=slug)
    new_product = None
    product_clothing = None
    
    if request.user.profile == store.owner:
        if request.method == 'GET':
            product_form = ProductForm()
            product_clothing_form = ProductClothingForm()
        if request.method == 'POST':
            product_form = ProductForm(data=request.POST)
            product_clothing_form = ProductClothingForm(data=request.POST)
            
            if product_form.is_valid() and product_clothing_form.is_valid():
                new_product = product_form.save(commit=False)
                product_clothing_form.save(commit=False)
                new_product.store = store
                new_product.owner = request.user.profile
                product = product_form.save()
                product = product
                product_clothing_form.product = product
                product_clothing_form.save()
                print(request.user.first_name)
                return redirect('/')
                

        context = {
            "object":store,
            "form": product_form,
            "product_clothing_form": product_clothing_form
        }
        return render(request, "store/store-dashboard.html", context)
    else:
        return redirect('store:store-detail-view', slug=store.slug)



嘗試改變這部分

        if product_form.is_valid() and product_clothing_form.is_valid():
            new_product = product_form.save(commit=False)
            product_clothing_form.save(commit=False)
            new_product.store = store
            new_product.owner = request.user.profile
            product = product_form.save()
            product = product
            product_clothing_form.product = product
            product_clothing_form.save()
            print(request.user.first_name)
            return redirect('/')

        if product_form.is_valid() and product_clothing_form.is_valid():
            new_product = product_form.save(commit=False)
            product_clothing_form = product_clothing_form.save(commit=False)
            new_product.store = store
            new_product.owner = request.user.profile
            new_product.save() #this is correct your mistake was here
            print(new_product)
            product_clothing_form.product = new_product
            product_clothing_form.save()
            print(request.user.first_name)
            return redirect('/')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM