简体   繁体   English

Django 具有基于类的视图的内联表单集

[英]Django inline formsets with Class-based view

I'm trying to do Django class-based CreateView inline formsets.我正在尝试做 Django 基于类的 CreateView 内联表单集。 I have a product model and a productImage model and productImage is inline everything looks fine but after i submit my product the images that are selected in formset will not save.我有一个产品 model 和一个 productImage model 和 productImage 是内联的,一切看起来都很好,但是在我提交我的产品后,在表单集中选择的图像将不会保存。 Here is my code这是我的代码

models.py:模型.py:

     class Product(models.Model):
        name=models.CharField(max_length=200, db_index=True),
        slug=models.SlugField(max_length=200, db_index=True, allow_unicode=True),
        description=models.TextField(blank=True),
        vector_column=SearchVectorField(null=True, editable=False),
        meta={"indexes": (GinIndex(fields=["vector_column"]),)}
        category = models.ForeignKey(Category, related_name='products', 
                   on_delete=models.CASCADE)
        price = models.DecimalField(max_digits=10, decimal_places=2)
        available = models.BooleanField(default=True)

        def get_absolute_url(self):
            return reverse('shop:product_detail', args=[self.id, self.slug])
        
     class ProductImage(models.Model):
        product = models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
        image = models.ImageField(upload_to='products/%y/%m/%d')

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

views.py视图.py

class ProductCreate(StaffAccessMixin, ProductFormValidMixin, ProductFieldsMixin,
                 CreateView):
model = Product

def get_context_data(self, **kwargs):
    context = super(ProductCreate, self).get_context_data(**kwargs)
    if self.request.POST:
        context['product_image_formset'] = ProductImageFormset(self.request.POST)
    else:
        context['product_image_formset'] = ProductImageFormset()
    return context


template_name = 'products/product-create-update.html'

ProductFormValidMixin: ProductFormValidMixin:

class ProductFormValidMixin():
  def form_valid(self, form):
    context = self.get_context_data()
    product_image_formset = context['product_image_formset']
    if self.request.user.is_superuser:
        self.obj = form.save()
        if product_image_formset.is_valid():
            product_image_formset.instance = self.obj
            product_image_formset.save()
        return redirect('administration:product-update', pk=self.obj.pk)
    else:
        self.obj = form.save(commit=False)
        self.obj.available = False
    return super().form_valid(form)

ProductFieldsMixin:产品领域混合:

class ProductFieldsMixin():
  def dispatch(self, request, *args, **kwargs):
    if request.user.is_superuser:
        self.fields = ["name",
                       "slug",
                       "description",
                       "category",
                       "price",
                       "available",
                       "image",
                       ]
    else:
        raise Http404
    return super().dispatch(request, *args, **kwargs)

forms.py: forms.py:

from django.forms.models import inlineformset_factory
from .models import Product, ProductImage

ProductImageFormset = inlineformset_factory(Product, ProductImage, fields=('image',), 
extra=3)

Formset template:表单集模板:

<h5 class="text-info">Add Product Metas</h5>
 {{ product_image_formset.management_form|crispy }}

  {% for form in product_image_formset.forms %}
       <tr class="{% cycle 'row1' 'row2' %} formset_row-{{product_image_formset.prefix }}">
         {% for field in form.visible_fields %}
                <td>
                    {# Include the hidden fields in the form #}
                     {% if forloop.first %}
                           {% for hidden in form.hidden_fields %}
                                 {{ hidden }}
                           {% endfor %}
                     {% endif %}
                     {{ field.errors.as_ul }}
                     {{ field|as_crispy_field }}
               </td>
          {% endfor %}
     </tr>
  {% endfor %}

Everything is ok and my product form is saved after submit but the images i select in my form set won't save一切正常,提交后我的产品表单已保存,但我的表单集中的图像我 select 不会保存

Problem solved!问题解决了!

This code will work for other types of data but not for images and files to solve this problem get_context_data function should be like this:此代码适用于其他类型的数据,但不适用于解决此问题的图像和文件 get_context_data function 应该是这样的:

def get_context_data(self, **kwargs):
    context = super(ProductCreate, self).get_context_data(**kwargs)
    if self.request.POST:
    context['product_image_formset']=ProductImageFormset(self.request.POST,
                                                         **self.request.FILES**)
    else:
        context['product_image_formset'] = ProductImageFormset()
    return context

In my first code self.request.FILES was missing:) and because of that images couldn't saved在我的第一个代码中 self.request.FILES 丢失:) 并且由于该图像无法保存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM