Problems adding field type SlugField (Django 1.6.3)
I want to share this case with you:
I have a model Albums
, Artists
andTracks
- One
Artist
can have manyAlbums
- One
Album
can have manyTracks
- Many
Tracks
are inside OneAlbum
(maybe ManyToMany too ..)
In the model, Albums
I want to add a type field SlugField
. This is the following:
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
I am executing migratios from the south:
(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 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)
If I join the team. /manage.py sqlall, the album model already appears with slug field in the database
(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;
But, when I go to the database, directly to the database structure that Django brings me, I see that the merge field is inefficient ... It can detail them in this url https://cldup.com/-F9SQ2D3W8.jpeg
To test if this pool is working, I create url "albums" that point to the class view 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'),
)
The class-based view is AlbumListView
as follows: Here I define a set of queries to restore artist albums , and the kwargs variable is the way to accept
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
When I go to view / albums in my browser I see this message:
no such column: albums_album.slug
This is an image of the error in my browser, check out this url:
What could be my problem? Why is migration not working? Thanks for your orientation :)
source to share
It looks like you added slug
to simulate AFTER start syncdb
and BEFORE you start convert_to_south
. Specifically, your southern app developer displays a warning on the console:
Note that South assumed that the application models match the database (ie, you haven't changed it since the last syncdb); if you have, you have to delete the album / migrations directory, return models.py it matches the database and try again.
To fix the problem, you need:
- Drop the SlugField from the album (look at the console output, you should also drop the artist)
- Transfer transfer folder from album app
- Run. /manage.py convert_to_south albums
- Run. /manage.py migrate albums
- Add SlugField
- Run. /manage.py albums-schemas -auto (specify default as '')
- Run. /manage.py migrate albums
- Profit!
source to share