Reputation:
my old models.py :
from django.db import models
from taggit.managers import TaggableManager
from django.utils.encoding import smart_unicode
import markdown
class PublishedManager(models.Manager):
def get_query_set(self):
return super(PublishedManager, self).get_query_set().filter\
(is_published=True)
class Category(models.Model):
name = models.CharField(max_length=55)
def __unicode__(self):
return u"%s" %(self.name)
class BookMarks(models.Model):
name = models.CharField(max_length=255)
url = models.URLField()
date_time = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u"%s %s %s" %(self.name, self.url, self.date_time)
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
excerpt = models.TextField(blank=True, help_text="A small teaser of\
your content")
content = models.TextField(blank=True, null=True)
contentmarkdown = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=True)
objects = models.Manager()
published_objects = PublishedManager()
tags = TaggableManager()
category = models.ForeignKey(Category)
def save(self):
self.content = markdown.markdown(self.contentmarkdown)
super(Post, self).save() # Call the "real" save() method.
class Meta:
ordering = ("date_created",)
def __unicode__(self):
return smart_unicode("%s %s %s %s %s" %(self.title, self.content, self.is_published, self.category, self.tags))
def get_absolute_url(self):
return "/posts/%s/" % self.id
class Book(models.Model):
name = models.CharField(max_length=55)
author = models.CharField(max_length=55)
image = models.ImageField(upload_to="static/img/books/")
body = models.TextField(blank=True, null=True)
bodymarkdown = models.TextField()
def __unicode__(self):
return u"%s %s" %(self.name, self.author)
def get_absolute_url(self):
return "/books/%s/" % self
.id
and after add "category = models.ForeignKey(Category)" this field to Book and BookMarks table, my new models.py like this:
from django.db import models
from taggit.managers import TaggableManager
from django.utils.encoding import smart_unicode
import markdown
class PublishedManager(models.Manager):
def get_query_set(self):
return super(PublishedManager, self).get_query_set().filter\
(is_published=True)
class Category(models.Model):
name = models.CharField(max_length=55)
def __unicode__(self):
return u"%s" %(self.name)
class BookMarks(models.Model):
name = models.CharField(max_length=255)
url = models.URLField()
date_time = models.DateTimeField(auto_now_add=True)
category = models.ForeignKey(Category)
def __unicode__(self):
return u"%s %s %s" %(self.name, self.url, self.date_time)
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
excerpt = models.TextField(blank=True, help_text="A small teaser of\
your content")
content = models.TextField(blank=True, null=True)
contentmarkdown = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=True)
objects = models.Manager()
published_objects = PublishedManager()
tags = TaggableManager()
category = models.ForeignKey(Category)
def save(self):
self.content = markdown.markdown(self.contentmarkdown)
super(Post, self).save() # Call the "real" save() method.
class Meta:
ordering = ("date_created",)
def __unicode__(self):
return smart_unicode("%s %s %s %s %s" %(self.title, self.content, self.is_published, self.category, self.tags))
def get_absolute_url(self):
return "/posts/%s/" % self.id
class Book(models.Model):
name = models.CharField(max_length=55)
author = models.CharField(max_length=55)
image = models.ImageField(upload_to="static/img/books/")
body = models.TextField(blank=True, null=True)
bodymarkdown = models.TextField()
category = models.ForeignKey(Category)
def __unicode__(self):
return u"%s %s" %(self.name, self.author)
def get_absolute_url(self):
return "/books/%s/" % self.id
class Errors(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
body = models.TextField(blank=True, null=True)
bodymarkdown = models.TextField()
def __unicode__(self):
return self.name
def get_absolute_url(self):
return "/errors/%s/" % self.id
I used south. Everything was going OK but after this line:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception.
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> datetime.datetime.now()
I get an error like this:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle
ignore_ghosts = ignore_ghosts,
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/__init__.py", line 213, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 235, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 310, in migrate_many
result = self.migrate(migration, database)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
result = self.run(migration)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 106, in run
dry_run.run_migration(migration)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 191, in run_migration
self._run_migration(migration)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 178, in _run_migration
raise exceptions.FailedDryRun(migration, sys.exc_info())
south.exceptions.FailedDryRun: ! Error found during dry run of '0012_auto__del_field_book_category__add_field_book_category1__del_field_boo'! Aborting.
Traceback (most recent call last):
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 175, in _run_migration
migration_function()
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda>
return (lambda: direction(orm))
File "/home/ada/mainproject/blog/migrations/0012_auto__del_field_book_category__add_field_book_category1__del_field_boo.py", line 17, in forwards
keep_default=False)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 44, in _cache_clear
return func(self, table, *args, **opts)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 402, in add_column
sql = self.column_sql(table_name, name, field)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/south/db/generic.py", line 688, in column_sql
default = field.get_db_prep_save(default, connection=self._get_connection())
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 991, in get_db_prep_save
connection=connection)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 292, in get_db_prep_save
prepared=False)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 284, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/ada/virtualenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 537, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'datetime.datetime'
Please can you tell me Why I get this error? What must I do?
Upvotes: 0
Views: 871
Reputation: 13308
Because your new category field is mandatory (you can't create a book or bookmark without giving it a category), south asked you what to do with the existing books and bookmarks in your database - your latest changes would have made all your current books / bookmarks invalid (since they don't have a category).
Your new category field is represented in your book/bookmark tables using a primary key of the category table. This primary key will likely be an integer (or possibly a string).
South asked you to supply a default primary key. Instead of supplying the primary key of an category object in your database (which would be an integer or a string). You've supplied a datatime object.
It's crapping out when it actually runs the migration because the field doesn't hold datetime objects. It holds integers (or strings). you need to create the migration again and add a valid primary key.
Upvotes: 1