2017-10-09 20 views
0

这里无效关键字的说法是错误:“形象”是本函数

'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 
+0

后全回溯请。 – Selcuk

+1

编辑,谢谢你的眼睛 – Chris

回答

1

您是拼写错误的字段名称,因为它不是imageproduct_image。更改以下行:

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

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