Vémundr Vémundr - 1 year ago 59
Python Question

save() got an unexpected keyword argument

Calling LocationDescription.l_edit returns error "save() got an unexpected keyword argument 'location'". Keyword name looks random though and may point to different fields at different times. The method l_edit was stripped of functionality, but the error persists. Curiously, self.location = kwargs['location'] followed by self.save() works just fine.


class LocationDescription(models.Model):
location = models.ForeignKey(Location)
description = models.ForeignKey(Localization)
(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)
(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)

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

def l_delete(self):
l = self.description

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

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'))
raise KeyError
for r in result:
if r.text is None or r.text == '':
setattr(r, 'text', 'Error: localization text missing!')
return result


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', '')))
year=request.POST.get('year', ''),
day=request.POST.get('day', ''),
order=request.POST.get('order', ''),
code_check=request.POST.get('code_check', ''),

Answer Source

save does not require those named arguments you're passing. Besides, since you're not overriding the default save method I don't see the need for super.

You can simply set those attributes on that instance of your model and call save like would with a model object:

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

On a side note, using update is more efficient than your current approach if you don't need to have the item in memory.