我想與大家分享這種情況下:問題將SlugField字段類型(Django的1.6.3)
我有機型Albums
,Artists
和Tracks
- 一個
Artist
可能有很多Albums
- 其中一個
Album
可能有許多Tracks
- 許多
Tracks
都在裏面一個Album
(可能是多對多太..)
在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
我以南執行migratios:
(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「專輯」,它指向基於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變量是如何利用
當我去查看/在我的瀏覽器的專輯,我看到這條消息:
no such column: albums_album.slug
這是錯誤的瀏覽器中的圖像,看看這個網址請:
什麼是我的問題?爲什麼遷移不起作用? 感謝您的方向:)
亞歷克斯感謝您的方向。您的支持非常有用 – bgarcial