相关疑难解决方法(0)

在Django admin中隐藏字段

我想通过设置editable = False来隐藏管理员中的slug字段,但每次我这样做时都会收到以下错误:

KeyError at /admin/website/program/6/
Key 'slug' not found in Form
Request Method: GET
Request URL:    http://localhost:8000/admin/website/program/6/
Exception Type: KeyError
Exception Value:    
Key 'slug' not found in Form
Exception Location: c:\Python26\lib\site-packages\django\forms\forms.py in __getitem__, line 105
Python Executable:  c:\Python26\python.exe
Python Version: 2.6.4
Run Code Online (Sandbox Code Playgroud)

不知道为什么会这样

django field admin

5
推荐指数
2
解决办法
9044
查看次数

django.db.utils.IntegrityError:唯一约束失败:rango_page__new.category_id

当我想运行此命令时遇到问题

python manage.py 迁移

我得到了错误...我试图搜索如何修复它,但我没有找到如何修复,也许这里有人可以帮助我解决这个错误,这是我得到的错误

Operations to perform:
  Apply all migrations: admin, rango, contenttypes, auth, sessions
Running migrations:
  Applying rango.0004_auto_20160107_0149...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-1.7.11-py2.7.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7.11-py2.7.egg/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7.11-py2.7.egg/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7.11-py2.7.egg/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7.11-py2.7.egg/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/Django-1.7.11-py2.7.egg/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, …
Run Code Online (Sandbox Code Playgroud)

python django

5
推荐指数
1
解决办法
3993
查看次数

UNIQUE 约束失败

我正在使用 Django 进行 Tango,但无法解决这个练习。我收到django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category.name错误。这是在我尝试实现对象views的属性之后Category。这是我的数据库填充脚本的摘录。我想也许我应该makemigrations然后migrate更新数据库模型。然而,这并没有帮助。

    cats = {
    'Python' : {'pages': python_pages, 'views':128},
    'Django': {'pages': django_pages, 'views':128},
    "Other Frameworks": {'pages':other_pages, 'views':128},
    }

for cat, cat_data in cats.items():
    c = add_cat(cat, cat_data['views'])
    for p in cat_data["pages"]:
        add_page(c, p['title'], p['url'])

for c in Category.objects.all():
    for p in Page.objects.filter(category=c):
        print("- {0} - {1}".format(str(c), str(p)))

def add_cat(name, views):
    c = Category.objects.get_or_create(name=name, views=views)[0]
    c.views=views
    c.save()
    return c
Run Code Online (Sandbox Code Playgroud)

添加Category模型:

class Category(models.Model): …
Run Code Online (Sandbox Code Playgroud)

django

4
推荐指数
1
解决办法
8475
查看次数

标签 统计

django ×3

admin ×1

field ×1

python ×1