2016-07-29 61 views
1

調用LocationDescription.l_edit返回錯誤「save()得到了一個意外的關鍵字參數'location'」。關鍵字名稱看起來是隨機的,並可能指向不同時間的不同字段。 l_edit方法被剝離了功能,但錯誤仍然存​​在。奇怪的是,self.location = kwargs ['location']後面跟着self.save()工作得很好。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', ''), 
        ) 
+0

嗯,這是因爲保存並不確實需要現場參數。你爲什麼這樣做? –

+0

因爲我不知道我在做什麼:)我將不得不再次閱讀這些例子。 –

回答

3

save不需要你傳遞的命名參數。此外,由於您不覆蓋默認的save方法,我不認爲需要super

您可以簡單地設置您的型號的該實例的屬性和調用save像會與一個模型對象:

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

在一個側面說明,使用update比你目前的做法更有效的,如果你不內存中不需要item

+0

謝謝,船長。我知道我在問一個愚蠢的問題,但我非常渴望到達Google的第二頁,並開始覺得我對某些非常基本的東西沒有理解。 –

相關問題