2017-07-26 63 views
0

我在我的博客上工作,但是當我遷移時,它給了我一個錯誤:「期望的字符串或緩衝區」錯誤。在第22行中,這是一個收集視圖時間的整體模型,我想也許它已經改變了view.py中的值,並且在創建它們之後可能無法修改模型。 因此,我試圖修改default並在view_time中添加其他參數,但這不起作用。有人可以幫我解決問題,或者給我一個明智的想法來重新設計一個更好的數據庫嗎?這是我的代碼。Django「期望的字符串或緩衝區」錯誤

models.py,Article包含view.py的view time

class Article(models.Model): 
    title = models.CharField(max_length=20) 
    content = MarkdownField() 
    date = models.DateField() 
    description = models.TextField(blank=True) 
    article_type = (
     ('Learning', (
      ('Python', 'python'), 
      ('Java', 'java'), 
     ) 
     ), 
    ('Life', 'life'), 
    ('Other', 'other') 
) 
    type = models.CharField(max_length=20, choices=article_type, blank=True) 
    lock = models.BooleanField(default=False) 
    view_time = models.IntegerField(default=1) # line 22 [error occured here] 

    def __str__(self): 
     return self.title 

class Comment(models.Model): 
    name = models.CharField(null=True, blank=True, max_length=20) 
    comment = models.TextField() 
    auto_date = models.DateField(auto_now_add=True) 
    belong_to = models.ForeignKey(to=Article, related_name="under_comment", null=True, blank=True) 

    def __str__(self): 
     return self.name 

部分,當一篇文章中被訪問時,view_time將增加1

def blog_content(request, page_num, error_form=None): 
    context = {} 
    form = CommentForm 
    view_update = Article.objects.get(id=page_num) 
    view_update.view_time += 1 
    view_update.save() 
    context['detail'] = view_update 
    comments = Comment.objects.filter(belong_to=view_update) 
    context['comments'] = comments 
    print(comments.values().count()) 
    if error_form is not None: 
     context['form'] = error_form 
    else: 
     context['form'] = form 
    return render(request, 'blog_content.html', context) 

的錯誤跟着

Applying blogapp.0018_comment_date...Traceback (most recent call last):##new added 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line 
    utility.execute() 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute 
    output = self.handle(*args, **options) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 202, in handle 
    targets, plan, fake=fake, fake_initial=fake_initial 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 97, in migrate 
    state = self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 132, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 237, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 129, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards 
    field, 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 231, in add_field 
    self._remake_table(model, create_fields=[field]) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table 
    self.effective_default(field) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 221, in effective_default 
    default = field.get_db_prep_save(default, self.connection) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 755, in get_db_prep_save 
    prepared=False) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1280, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1275, in get_prep_value 
    return self.to_python(value) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 1237, in to_python 
    parsed = parse_date(value) 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/dateparse.py", line 60, in parse_date 
    match = date_re.match(value) 
TypeError: expected string or buffer 

這是blogapp.0018_comment_date文件:

# -*- coding: utf-8 -*- 
# Generated by Django 1.10 on 2017-07-26 12:19 
from __future__ import unicode_literals 

from django.db import migrations, models 


class Migration(migrations.Migration): 

    dependencies = [ 
     ('blogapp', '0017_comment'), 
    ] 

    operations = [ 
     migrations.AddField(
      model_name='comment', 
      name='date', 
      field=models.DateField(default=True), 
     ), 
    ] 
+0

這個錯誤發生在遷移中,它與視圖無關。 –

+0

那我該如何解決? – Pan

+0

請顯示失敗的遷移。 – Alasdair

回答

1

choices應該是元組的迭代。你有三個層次的結構。

試試這個:

class Article(models.Model): 
    title = models.CharField(max_length=20) 
    content = MarkdownField() 
    date = models.DateField() 
    description = models.TextField(blank=True) 
    article_type = (
     ('Learning-Python', 'python'), 
     ('Learning-Java', 'java'), 
     ('Life', 'life'), 
     ('Other', 'other') 
    ) 
    type = models.CharField(max_length=20, choices=article_type, blank=True) 
    lock = models.BooleanField(default=False) 
    view_time = models.IntegerField(default=1) 
如果你想類別添加到

article_type,你需要將其添加爲一個屬性。

希望這會有所幫助。

+0

這是不正確的。 [docs](https://docs.djangoproject.com/en/1.11/ref/models/fields/#choices)顯示瞭如何將選擇分組到命名組。 – Alasdair

+0

@阿拉斯加。哇。我不知道。謝謝! –

+0

這是真的,但真正的問題是'view_time = models.IntegerField(default = 1)',如果你知道如何解決它,我將不勝感激。 – Pan

相關問題