编程语言
首页 > 编程语言> > python-添加SlugField字段类型时遇到问题(Django 1.6.3)

python-添加SlugField字段类型时遇到问题(Django 1.6.3)

作者:互联网

我想与您分享这种情况:

我有模特专辑,艺术家和曲目

>一位艺术家可能有很多专辑
>一张专辑可能有很多曲目
>许多曲目都在一张专辑中(也可能是ManyToMany ..)

在相册模型中,我要添加一个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,则专辑模型已经与数据库的子弹字段一起出现

(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字段无效…这可以在此URL https://cldup.com/-F9SQ2D3W8.jpeg中详细介绍它们

为了测试该条是否有效,我创建了URL“相册”,该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如下:在这里,我定义了一个查询集,用于恢复艺术家的专辑,并且使用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

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

我有什么问题?为什么迁移不起作用?
感谢您的介绍:)

解决方法:

看起来您在运行syncdb之后和运行convert_to_south之前为模型添加了子弹.特别是关于您的案例,South的开发人员会在控制台中显示警告消息:

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.

为了解决您的问题,您应该:

>从专辑中删除SlugField(在控制台输出中看起来,您也应该删除艺术家)
>从相册应用中删除迁移文件夹
>运行./manage.py convert_to_south相册
>运行./manage.py迁移相册
>添加SlugField
>运行./manage.py schemamigration albums –auto(将默认值指定为”)
>运行./manage.py迁移相册
>利润!

标签:slug,python,django
来源: https://codeday.me/bug/20191029/1956311.html