【问题标题】:Django save function error - int() argument must be a string or a number, not 'Template'Django 保存函数错误 - int() 参数必须是字符串或数字,而不是“模板”
【发布时间】:2018-11-09 19:16:53
【问题描述】:

我正在构建一个 Django 应用程序。 当我尝试保存我的模型类时,我收到以下错误消息

int() 参数必须是字符串或数字,而不是“模板”

我的模型好像是这样的。

class Template(models.Model):
  creation_time = models.DateTimeField(null=True,default=datetime.datetime.now)
  type = models.CharField(max_length=10,blank=True,null=True)

def save(self,info=None):
  super(Template,self).save()
  if info:
    print 'info'

在我看来,我的函数调用似乎是这样的

template = Template(creation_time=datetime.datetime.now(),type='html')
template.save(info='Front Page')

【问题讨论】:

  • 你用Content调用,而不是Template,super(..)调用是(可能是复制的)错误的方式。
  • 没有缩进问题,我又重写了保存功能,但还是有同样的问题

标签: django django-models


【解决方案1】:

我在修复它与 django 2.1 一起使用后尝试您的代码

class Template(models.Model):
    creation_time = models.DateTimeField(null=True, default=datetime.datetime.now)
    type = models.CharField(max_length=10, blank=True, null=True)

    def save(self, info=None):
        super(Template, self).save()
        if info:
            print(info)

你忘记添加 models.CharField

但是使用“type”作为变量名是个坏主意,这个变量名被python内置使用

>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError',
 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError',
 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError',
 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError',
 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError',
 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning',
 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError',
 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration',
 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError',
 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_',
 '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__',
 '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool',
 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex',
 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval',
 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr',
 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int',
 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map',
 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow',
 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set',
 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
 'type', 'vars', 'zip']

【讨论】:

  • 在更新它之后,我收到了同样的错误信息。我也用你的回答更新了问题。
  • 我使用的是 Django 1.11.11
  • 即使在将我的 Django 更新到 1.11.16 之后,我也遇到了同样的错误
猜你喜欢
  • 1970-01-01
  • 2014-02-26
  • 2012-08-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多