2016-07-29 8 views
1

Calling 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

saveは実際にフィールド引数を取らないためです。なぜあなたはそれをこのようにしていますか? –

+0

私は何をしているのかわからないので、私は再び例を読む必要があります。 –

答えて

3

saveはあなたが渡しているこれらの名前付き引数を必要としません。また、デフォルトのsaveメソッドをオーバーライドしていないため、superの必要はありません。

あなたは、単にあなたのモデルのインスタンスにそれらの属性を設定し、モデルオブジェクトの場合と同じようsaveを呼び出すことができます。updateは、あなたの現在のアプローチよりも効率的である使用して、サイドノートで

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

もしあなたドンメモリにitemが必要です。

+0

ありがとう、キャプテン。私は愚かな質問をしていることを知っていましたが、私はGoogleの第2ページに到達するのには絶望的でした。私は非常に基本的なことを理解していないと感じ始めました。 –

関連する問題