python - save() 收到意外的关键字参数

标签 python django

调用 LocationDescription.l_edit 返回错误“save() 获得意外的关键字参数‘位置’”。关键字名称看起来是随机的,并且可能在不同时间指向不同的字段。方法 l_edit 被剥夺了功能,但错误仍然存​​在。奇怪的是, self.location = kwargs['location'] 后面跟着 self.save() 效果很好。

模型.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

View .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 存储在内存中,则比您当前的方法更有效。

关于python - save() 收到意外的关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38660128/

相关文章:

python - 如何使用 Django 1.11 基于模板的表单渲染将 CSS 类添加到小部件/字段

python - 在另一个框架中调用函数 (WxPython)

python - matplotlib 中同一循环的两个不同图?

python - 是否可以将参数传递给 Django 模板中的模型类方法?

python - Django 迁移或迁移时出错

python - mpi4py Reduce() 中可能的缓冲区大小限制

python - django 以每周每月每天的方式对查询元素进行排序

Django 说端口已被使用

python - 如何创建包含不同类型的查询集表达式

python - Django REST 嵌套路由器