- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试遵循 tangowithdjango 这本书,并且必须添加一个 slug 来更新类别表。但是,我在尝试迁移数据库后遇到错误。
http://www.tangowithdjango.com/book17/chapters/models_templates.html#creating-a-details-page
我没有为 slug 提供默认值,所以 Django 要求我提供一个默认值,并按照书上的指示输入 ''。
值得注意的是,我没有像原书中那样使用 sqlite,而是使用 mysql。
models.py
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = "Categories"
def __unicode__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
命令提示符
sudo python manage.py migrate
Operations to perform:
Apply all migrations: admin, rango, contenttypes, auth, sessions
Running migrations:
Applying rango.0003_category_slug...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 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/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/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/schema.py", line 42, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 411, in add_field
self.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 98, in execute
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 128, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'slug'")
最佳答案
我们一步步来分析:
slug
字段 unique = True
, 即:每条记录必须有不同的值, slug
中不能有两条记录具有相同的值slug
中没有其他记录有空字符串领域slug
中已经有空字符串 field 。引发异常并中止迁移。这就是您的迁移失败的原因。你应该做的就是编辑迁移,复制migrations.AlterField
操作两次,在第一次操作中删除 unique=True。在这些操作之间你应该放 migrations.RunPython
操作并为其提供 2 个参数:generate_slugs
和 migrations.RunPython.noop
.
现在您必须在迁移类之前在迁移函数中创建,将该函数命名为 generate_slugs
.函数应采用 2 个参数:apps
和 schema_editor
.在你的函数放在第一行:
Category = apps.get_model('your_app_name', 'Category')
现在使用 Category.objects.all()
循环所有记录并为每个记录提供唯一的 slug。
关于python - django.db.utils.IntegrityError : (1062, "Duplicate entry ' ' 键 'slug' "),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383766/
我仅使用 slug 来标识网站上的页面,例如: example.tld/view/this-fancy-slug 。这是使用此函数自动从标题生成的: public static function No
哪个是新闻页面上 url 的最佳选择: 动态生成 slug。从对象 ID 加载页面。如果 slug 不匹配,永久重定向到正确的 slug。 myweb.com/542/my-news-item 我看到
slug 是描述或标题页面的 URL 的一部分,通常是该页面的关键字丰富,可改善 SEO。例如在这个网址 PHP/JS - Create thumbnails on the fly or store
刚开始玩 Django,发现了一个链接 here关于如何创建 slug。我被告知对现有模型执行以下更改: from django.template.defaultfilters import slug
我有 2 个自定义帖子类型的帖子。视频帖子和城市指南帖子第一个帖子(视频帖子)包含 url:104.130.239.132/rick-owens/第二个帖子(城市指南)conatins url:htt
我正在创建一个需要 slug 系统的系统。例如,标题为“博客标题:此处”的博客文章的 URL 为: YYYY/MM/DD/the-blog-title-here 我的系统设置得很好,如果您的 URL
我已使用以下代码注册自定义帖子类型: register_post_type( array( 'labels' => // array o
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
我刚刚安装了使用 Sluggable 的学说扩展。 我做这个: Composer .json "stof/doctrine-extensions-bundle": "1.2.*@dev" 应用内核.p
我在为一个私有(private)网站工作。我有 2 个表: 表 A(包含较旧的 slug)。 表 B(新 slug)。 这里的代码用于检查是否存在 slug 并从表 B(单表查询)创建唯一的 slug
我在我的项目中使用了 ui-router,我在文档中看到了两者: .state('app.restaurants.index', { url: '/{slug}', contr
CREATE TABLE news ( id int(11) NOT NULL AUTO_INCREMENT, title varchar (128) NOT NULL, sl
我想转换任何标题,例如一个用户友好的 url 的博客条目。我用了rawurlencode()这样做但它给了我很多奇怪的字符串,比如 %s。 该算法应考虑 Ö、Ä 等德语字符。我想从标题创建一个 url
我想捕获 URL 中的第一个 slug仅限 如果 URL 有一个 slug,如果它在 URL 中有多个 slug 或部分,则忽略它。例如: https://example.com/path/some-
我有一种情况,在 url 的底部我有一个动态 slug 来获取数据。现在我发现我需要一个静态 slug,这将表示一个不同的页面,但仍然可以访问基本动态 slug 以获取信息。 示例路径 /formAB
我正在开发一个简单的博客来学习 Django。我希望每个帖子都有一个这样的路径: /category-1/title-post /category-2/title-post etc.. 低于urls.
该网站非常简单,只有 3 页,主页和 urls.py 中显示的第二个 url 效果很好,但是当我从主题转到单元页面时,我得到: TypeError at /subjects/units/english
在我一直在开发的应用程序中,我遇到了像 /books/:slug, :to => 'books#show', slug:/.*?/ 这样的路由。我很好奇这部分的作用 slug:/.*?/ ? 最佳答案
我尝试使用此查询更改特定帖子的帖子别名: UPDATE wp_posts SET post_name = replace(post_name, 'make-me-happy','make-me-hap
我尝试使用 HarpJS,一个 NodeJS 静态博客生成器。在教程中有这样的元数据示例: for article, slug in public.articles._data a(href="/a
我是一名优秀的程序员,十分优秀!