添加SlugField字段类型的问题(Django 1.6.3)

时间:2014-11-16 02:24:06

标签: python django slug

我想和你分享这个案子:

我有模型AlbumsArtistsTracks

  • 一个 Artist可能很多 Albums
  • 一个 Album可能很多 Tracks
  • 许多 Tracks位于一个 Album内(也可能 ManyToMany )。

在我想要的Albums模型中添加SlugField类型的字段。这是以下内容:

from django.db import models
from artists.models import Artists

    class Album(models.Model):
        title = models.CharField(max_length=255)
        cover = models.ImageField(upload_to='albums') 
        slug = models.SlugField(max_length=100)
        artist = models.ForeignKey(Artists)

        def __unicode__(self):
            return self.title

我在南方进行迁移:

(myvenv)➜  myvenv  ./manage.py syncdb

Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
> django.contrib.admin
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.messages
> django.contrib.staticfiles
> south
> albums

Not synced (use migrations):
- django_extensions
- djcelery
- tracks
- artists
- userprofiles
(use ./manage.py migrate to migrate these)

(myenv)➜  myenv  ./manage.py convert_to_south albums

Creating migrations directory at '/home/bgarcial/workspace/myenv/sfotipy/albums/migrations'...
Creating __init__.py in '/home/bgarcial/workspace/myenv/sfotipy/albums/migrations'...
+ Added model albums.Album
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate albums
- Soft matched migration 0001 to 0001_initial.
Running migrations for albums:
- Nothing to migrate.
- Loading initial data for albums.
Installed 0 object(s) from 0 fixture(s)

App 'albums' converted. Note that South assumed the application's models matched the   database
(i.e. you haven't changed it since last syncdb); if you have, you should delete  the  albums/migrations directory, revert models.py so it matches the database, and try again.

(myenv)➜  myenv  ./manage.py migrate albums         

Running migrations for albums:
- Nothing to migrate.
- Loading initial data for albums.
Installed 0 object(s) from 0 fixture(s) 

如果我输入命令./manage.py sqlall,相册模型已经出现在数据库中的slug字段

(Sfoti.py)➜  sfotipy  ./manage.py sqlall albums
BEGIN;
CREATE TABLE "albums_album" (
    "id" integer NOT NULL PRIMARY KEY,
    "title" varchar(255) NOT NULL,
    "cover" varchar(100) NOT NULL,
    "slug" varchar(100) NOT NULL,
    "artist_id" integer NOT NULL REFERENCES "artists_artists" ("id")
);
CREATE INDEX "albums_album_f52cfca0" ON "albums_album" ("slug");
CREATE INDEX "albums_album_7904f807" ON "albums_album" ("artist_id");
COMMIT; 

但是,当我进入数据库,直接进入Django带给我的数据库结构时,我发现slug字段无效......这可以在这个网址中详细说明https://cldup.com/-F9SQ2D3W8.jpeg

为了测试这个slu works的作用,我创建了url"专辑"指向AlbumListView基于类的视图

from django.conf.urls import patterns, url
from artists.views import AlbumListView

urlpatterns = patterns('',
    url(r'^albums/$', AlbumListView.as_view(), name='album_list'),
    url(r'^albums/(?P<artist>[\w\-]+)/$', AlbumListView.as_view(), name='album_list'),
)

基于类的视图AlbumListView如下:在此我定义了一个 queryset ,用于恢复艺术家相册,使用 kwargs 变量是如何获取

的方式
class AlbumListView(ListView):
    model = Album
    template_name = 'album_list.html'

    def get_queryset(self):
        if self.kwargs.get('artist'):
            queryset = self.model.objects.filter(artist__slug=self.kwargs['artist'])
        else:
            queryset = super(AlbumListView, self).get_queryset()
        return queryset

当我在浏览器中转到视图/相册时,我会看到以下消息:

no such column: albums_album.slug

这是我浏览器中的错误图片,请查看此网址:

enter image description here

我的问题是什么?为什么迁移不起作用? 谢谢你的指导:)

1 个答案:

答案 0 :(得分:4)

您似乎已将slug添加到模型 AFTER 运行syncdb之前运行convert_to_south。特别是关于你的案例,南方开发者在控制台中显示一条警告信息:

  

请注意,南方假设应用程序的模型与之匹配   数据库(即自上次syncdb以来你没有改变它);如果你有,   你应该删除专辑/迁移目录,还原models.py所以   它匹配数据库,然后再试一次。

要解决问题,您应该:

  • 从专辑中删除SlugField(在您的控制台输出中查找,您也应该删除艺术家)
  • 从相册应用
  • 中删除迁移文件夹
  • 运行./manage.py convert_to_south相册
  • 运行./manage.py迁移相册
  • 添加SlugField
  • 运行./manage.py架构移动相册--auto(将默认值指定为'')
  • 运行./manage.py迁移相册
  • 利润!