繁体   English   中英

save()得到了一个意外的关键字参数

[英]save() got an unexpected keyword argument

调用LocationDescription.l_edit会返回错误“save()得到一个意外的关键字参数'location'”。 关键字名称看起来是随机的,并且可能在不同时间指向不同的字段。 l_edit方法被剥夺了功能,但错误仍然存​​在。 奇怪的是,self.location = kwargs ['location']后跟self.save()工作正常。

models.py

class LocationDescription(models.Model):
    location = models.ForeignKey(Location)
    description = models.ForeignKey(Localization)
    YEAR_CHOICES = (
        (LocationDescriptionYear.any.value, 'Any'),
        (LocationDescriptionYear.winter.value, 'Winter'),
        (LocationDescriptionYear.spring.value, 'Spring'),
        (LocationDescriptionYear.summer.value, 'Summer'),
        (LocationDescriptionYear.autumn.value, 'Autumn'),
    )
    year = models.IntegerField(choices=YEAR_CHOICES, default=0)
    DAY_CHOICES = (
        (LocationDescriptionDaytime.any.value, 'Any'),
        (LocationDescriptionDaytime.night.value, 'Night'),
        (LocationDescriptionDaytime.morning.value, 'Morning'),
        (LocationDescriptionDaytime.day.value, 'Day'),
        (LocationDescriptionDaytime.evening.value, 'Evening'),
    )
    day = models.IntegerField(choices=DAY_CHOICES, default=0)
    weather_type = models.ForeignKey('Weather', blank=True, null=True)
    order = models.IntegerField(default=0)
    code_check = models.TextField(blank=True, null=True)

    @classmethod
    def l_create(cls, request, **kwargs):
        l = Localization()
        l.write(request, kwargs['description'])
        kwargs['description'] = l
        item = cls(**kwargs)
        item.save()
        return item

    def l_delete(self):
        l = self.description
        self.delete()
        l.delete()

    def l_edit(self, **kwargs):
        super(LocationDescription, self).save(**kwargs)

    @classmethod
    def localize(cls, locale, **kwargs):
        if locale == 'eng':
            result = cls.objects.filter(**kwargs).annotate(text=F('description__eng'))
        elif locale == 'rus':
            result = cls.objects.filter(**kwargs).annotate(text=F('description__rus'))
        else:
            raise KeyError
        for r in result:
            if r.text is None or r.text == '':
                setattr(r, 'text', 'Error: localization text missing!')
        return result

views.py

            location = Location.objects.get(pk=int(request.POST.get('location', '')))
            weather_type = Weather.objects.get(pk=int(request.POST.get('weather_type', '')))
            item = LocationDescription.objects.get(pk=int(request.POST.get('id', '')))
            item.l_edit(location=location,
                        year=request.POST.get('year', ''),
                    day=request.POST.get('day', ''),
                    weather_type=weather_type,
                    order=request.POST.get('order', ''),
                    code_check=request.POST.get('code_check', ''),
                    )

save不需要你传递的那些命名参数。 此外,由于你没有覆盖默认的save方法,我认为不需要super

您可以在模型的该实例上简单地设置这些属性,并像模型对象一样调用save

def l_edit(self, **kwargs):
    for k in kwargs:
        setattr(self, k, kwargs[k])
    self.save()

在一个侧面说明,使用update比你目前的做法更有效的,如果你不需要有item在内存中。

暂无
暂无

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

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