models.py
from __future__ import unicode_literals
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.text import slugify
class Post(models.Model):
title = models.CharField(max_length = 120)
image = models.ImageField(null=True,blank=True)
draft = models.BooleanField(default=False)
publish = models.DateField(auto_now=False, auto_now_add=False)
Updated = models.DateTimeField(auto_now=True,auto_now_add=False)
Timestamp = models.DateTimeField(auto_now=False,auto_now_add=True)
def __unicodde__(self):
return self.Title
def __str__(self):
return self.title
def get_absolute_url(self):
#return 'posts/%s/' %(self.id)
return reverse('posts:detail', kwargs={'id':self.id})
class Meta:
ordering = ["-Timestamp","Updated"]
forms.py
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = [
"title",
"Content",
"image",
"draft",
"publish",
]
和终端:
jayu@broadwell-gt2:~/Desktop/trydjango19$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, contenttypes, posts, auth, sessions
Running migrations:
Rendering model states... DONE
Applying posts.0002_auto_20180526_0745...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 221, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 103, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1301, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1296, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1260, in to_python
parsed = parse_date(value)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateparse.py", line 60, in parse_date
match = date_re.match(value)
TypeError: expected string or buffer
成功完成python manage.py makemigrations后,
当我尝试迁移时
TypeError:期望的字符串或缓冲区 发生错误。
我在数据类型上做过任何错误或其他错误吗?
我在这里做错了什么?
请帮帮我......
模型字段中的任何默认值出错?
答案 0 :(得分:0)
我认为django docs关于DateTimeField的引用可以帮到你:
选项auto_now_add,auto_now和default是互斥的。这些选项的任何组合都会导致错误。