2014-11-16 68 views
3

我想與大家分享這種情況下:問題將SlugField字段類型(Django的1.6.3)

我有機型AlbumsArtistsTracks

  • 一個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 

這是錯誤的瀏覽器中的圖像,看看這個網址請:

enter image description here

什麼是我的問題?爲什麼遷移不起作用? 感謝您的方向:)

回答

4

看起來你添加slug模型運行syncdb運行convert_to_south。特別是關於你的情況以南的開發者顯示在控制檯的警告消息:

需要注意的是南假定應用程序的模型相匹配的 數據庫(即你還沒有從去年執行syncdb改變了它);如果你有, 你應該刪除專輯/ migrations目錄,恢復models.py所以 它匹配數據庫,然後再試一次。

用於固定您的問題,您應該:

  • 從相冊中刪除SlugField(看在你的控制檯輸出,你應該放下藝術家太)從相冊應用
  • 運行
  • 刪除遷移的文件夾。 /manage.py convert_to_south專輯
  • 運行./manage.py遷移專輯
  • 添加SlugField
  • 運行./manage.py模式遷移相冊--auto(指定默認值爲'')
  • 運行./manage.py遷移相冊
  • 獲利!
+0

亞歷克斯感謝您的方向。您的支持非常有用 – bgarcial