【问题标题】:Django Setup:TypeError: 'pub_date' is an invalid keyword argument for this functionDjango 设置:TypeError:'pub_date' 是此函数的无效关键字参数
【发布时间】:2016-02-11 06:57:34
【问题描述】:

在 django 设置过程中,我遇到了以下错误

引发 TypeError("'%s' 是此函数的无效关键字参数" % list(kwargs)[0])
TypeError: 'pub_date' 是此函数的无效关键字参数

Models.py

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
import datetime
from django.utils import timezone

# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

@python_2_unicode_compatible  # only if you need to support Python 2
class Question(models.Model):
    # ...
    def __str__(self):
        return self.question_text

@python_2_unicode_compatible  # only if you need to support Python 2
class Choice(models.Model):
    # ...
    def __str__(self):
        return self.choice_text

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

【问题讨论】:

    标签: django migration


    【解决方案1】:

    我认为您从这里盲目复制粘贴:https://docs.djangoproject.com/en/1.9/intro/tutorial02/。您需要正确阅读教程。所以你需要做的是像这样重写你的models.py

    from django.db import models
    
    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
    @python_2_unicode_compatible
    class Question(models.Model):
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
        def __str__(self):
            return self.question_text
    
        def was_published_recently(self):
            return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    
    @python_2_unicode_compatible
    class Choice(models.Model):
        question = models.ForeignKey(Question, on_delete=models.CASCADE)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
    
        def __str__(self):
            return self.choice_text
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2019-01-20
      相关资源
      最近更新 更多