'图像'是此函数的无效关键字参数

时间:2017-10-09 23:56:26

标签: django django-models django-forms django-views

这是错误:

'image' is an invalid keyword argument for this function

了解发生的事情以及如何解决问题的任何帮助都将非常感激。

基本上我有一个合作伙伴'与“产品”有一对多关系的模型'模型。我正在使用inlineFormSet作为'产品'模型,其中包含合作伙伴,图像,desc,价格等字段。

表单在管理员中正确显示,表单呈现时但提交时会抛出错误。

这是我的views.py

def partner_create(request):
#Trying to add multiple product functionality
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404

    ProductFormSet = inlineformset_factory(Partner, Product, form=ProductForm, extra=3)

    if request.method == 'POST':
        partnerForm = PartnerForm(request.POST or None, request.FILES or None)
        formset = ProductFormSet(request.POST, request.FILES, queryset=Product.objects.none())

        if partnerForm.is_valid() and formset.is_valid():

            instance = partnerForm.save(commit=False)
            instance.save()


            for form in formset.cleaned_data:
                image = form['image']
                product = Product(partner=instance, image=image)
                product.save()
            messages.success(request, "Partner Successfully Created")
        else:
            print partnerForm.errors, formset.errors
    else:
        partnerForm = PartnerForm()
        formset = ProductFormSet(queryset=Product.objects.none())
    return render(request, "partner_form.html", {"partnerForm": partnerForm, "formset": formset})

这是我的models.py

class Partner(models.Model):
    name = models.CharField(max_length=120)
    logo = models.ImageField(upload_to=upload_location, 
        null=True, 
        blank=True, 
        width_field="width_field", 
        height_field="height_field")
    banner_image = models.ImageField(upload_to=upload_location,
        null=True, 
        blank=True, 
        width_field="width_field", 
        height_field="height_field")
    mission = models.TextField()
    vision = models.TextField()
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)
    # text = models.TextField()
    website_link = models.CharField(max_length=120)
    fb_link = models.CharField(max_length=120)
    twitter_link = models.CharField(max_length=120)
    ig_link = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)


    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("partners:detail", kwargs={"slug": self.slug})
        # return "/partner/%s/" %(self.id)

    def get_markdown(self):
        mission = self.mission
        markdown_text = markdown(mission)
        return mark_safe(markdown_text)


#Creating a many to one relationship so that one can upload many Products
class Product(models.Model):
    partner = models.ForeignKey(Partner, default=None)
    name = models.CharField(max_length=120)
    product_image = models.ImageField(upload_to=upload_location,
    # product_image = models.ImageField(upload_to= (upload_location + '/' + name), Something like this need to append actual product name so these dont just get dumped in the media for partners
        null=True, 
        blank=True, 
        width_field="width_field", 
        height_field="height_field",
        verbose_name='Image',)
    description = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    height_field = models.IntegerField(default=0)
    width_field = models.IntegerField(default=0)

    def __unicode__(self):              # __unicode__ on Python 2
        return self.name

这是我的forms.py

class PartnerForm(forms.ModelForm):
    mission = forms.CharField(widget=PagedownWidget(show_preview=False))
    vision = forms.CharField(widget=PagedownWidget(show_preview=False))
    # publish = forms.DateField(widget=forms.SelectDateWidget)
    class Meta:
        model = Partner
        fields = [
            "name",
            "logo",
            "banner_image",
            "mission",
            "vision",
            "website_link",
            "fb_link",
            "twitter_link",
            "ig_link",
        ]

class ProductForm(forms.ModelForm):
    image = forms.ImageField(label='Image')
    class Meta:
        model = Product
        fields = [
            "partner",
            "image",
            "description",
            "price"
        ]

我理解问题出在views.py

中的这一行
product = Product(partner=instance, image=image)

但我不明白为什么我所做的并不起作用" image = image" (我以前见过这样做过)或者从这里开始。提前致谢

完整追溯:

File "...lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File ".../lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "...blog/src/partners/views.py" in partner_create
  49.               product = Product(partner=instance, image=image)

File ".../lib/python2.7/site-packages/django/db/models/base.py" in __init__
  443.                 raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])

Exception Type: TypeError at /partners/create/
Exception Value: 'image' is an invalid keyword argument for this function

1 个答案:

答案 0 :(得分:1)

您错误拼写了字段名称,因为它不是image,而是product_image。更改以下行:

product = Product(partner=instance, image=image)

product = Product(partner=instance, product_image=image)