私は T Stone によって概説された解決策を試してみましたが、それは素晴らしいスターターであり、物事をどのように行うべきかを説明していると思いますが、いくつかの問題に遭遇しました.
ほとんどの場合、親クラスのテーブル エントリをもう作成する必要はないと思います。つまり、必要ありません。
new_movie.videofile_ptr = orm['media.VideoFile'].objects.create()
もう。Djangoはこれを自動的に行うようになりました(null以外のフィールドがある場合、上記は機能せず、データベースエラーが発生しました)。
おそらくdjangoとsouthの変更が原因だと思います。これは、django 1.2.3とsouth 0.7.1を使用したubuntu 10.10で機能したバージョンです。モデルは少し異なりますが、要点は次のとおりです。
初期設定
post1/models.py:
class Author(models.Model):
first = models.CharField(max_length=30)
last = models.CharField(max_length=30)
class Tag(models.Model):
name = models.CharField(max_length=30, primary_key=True)
class Post(models.Model):
created_on = models.DateTimeField()
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag)
title = models.CharField(max_length=128, blank=True)
content = models.TextField(blank=True)
post2/models.py:
class Author(models.Model):
first = models.CharField(max_length=30)
middle = models.CharField(max_length=30)
last = models.CharField(max_length=30)
class Tag(models.Model):
name = models.CharField(max_length=30)
class Category(models.Model):
name = models.CharField(max_length=30)
class Post(models.Model):
created_on = models.DateTimeField()
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag)
title = models.CharField(max_length=128, blank=True)
content = models.TextField(blank=True)
extra_content = models.TextField(blank=True)
category = models.ForeignKey(Category)
明らかに多くの重複があるため、共通点を一般的な投稿モデルに分解し、他のモデル クラスの違いのみを保持したいと考えました。
新しいセットアップ:
genpost/models.py:
class Author(models.Model):
first = models.CharField(max_length=30)
middle = models.CharField(max_length=30, blank=True)
last = models.CharField(max_length=30)
class Tag(models.Model):
name = models.CharField(max_length=30, primary_key=True)
class Post(models.Model):
created_on = models.DateTimeField()
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag)
title = models.CharField(max_length=128, blank=True)
content = models.TextField(blank=True)
post1/models.py:
import genpost.models as gp
class SimplePost(gp.Post):
class Meta:
proxy = True
post2/models.py:
import genpost.models as gp
class Category(models.Model):
name = models.CharField(max_length=30)
class ExtPost(gp.Post):
extra_content = models.TextField(blank=True)
category = models.ForeignKey(Category)
先に進みたい場合は、まずこれらのモデルを南に移動する必要があります。
$./manage.py schemamigration post1 --initial
$./manage.py schemamigration post2 --initial
$./manage.py migrate
データの移行
それについてどうやって行くのですか?最初に新しいアプリ genpost を作成し、south で初期移行を行います。
$./manage.py schemamigration genpost --initial
($シェルプロンプトを表すために使用しているので、入力しないでください。)
次に、新しいクラスSimplePostとExtPostをそれぞれ post1/models.py と post2/models.py に作成します (残りのクラスはまだ削除しないでください)。次に、これら 2 つのスキーマ移行も作成します。
$./manage.py schemamigration post1 --auto
$./manage.py schemamigration post2 --auto
これで、これらすべての移行を適用できます。
$./manage.py migrate
post1 と post2 から genpost にデータを移行して、問題の核心に取り掛かりましょう。
$./manage.py datamigration genpost post1_and_post2_to_genpost --freeze post1 --freeze post2
次に、genpost/migrations/0002_post1_and_post2_to_genpost.py を編集します。
class Migration(DataMigration):
def forwards(self, orm):
#
# Migrate common data into the new genpost models
#
for auth1 in orm['post1.author'].objects.all():
new_auth = orm.Author()
new_auth.first = auth1.first
new_auth.last = auth1.last
new_auth.save()
for auth2 in orm['post2.author'].objects.all():
new_auth = orm.Author()
new_auth.first = auth2.first
new_auth.middle = auth2.middle
new_auth.last = auth2.last
new_auth.save()
for tag in orm['post1.tag'].objects.all():
new_tag = orm.Tag()
new_tag.name = tag.name
new_tag.save()
for tag in orm['post2.tag'].objects.all():
new_tag = orm.Tag()
new_tag.name = tag.name
new_tag.save()
for post1 in orm['post1.post'].objects.all():
new_genpost = orm.Post()
# Content
new_genpost.created_on = post1.created_on
new_genpost.title = post1.title
new_genpost.content = post1.content
# Foreign keys
new_genpost.author = orm['genpost.author'].objects.filter(\
first=post1.author.first,last=post1.author.last)[0]
new_genpost.save() # Needed for M2M updates
for tag in post1.tags.all():
new_genpost.tags.add(\
orm['genpost.tag'].objects.get(name=tag.name))
new_genpost.save()
post1.delete()
for post2 in orm['post2.post'].objects.all():
new_extpost = p2.ExtPost()
new_extpost.created_on = post2.created_on
new_extpost.title = post2.title
new_extpost.content = post2.content
# Foreign keys
new_extpost.author_id = orm['genpost.author'].objects.filter(\
first=post2.author.first,\
middle=post2.author.middle,\
last=post2.author.last)[0].id
new_extpost.extra_content = post2.extra_content
new_extpost.category_id = post2.category_id
# M2M fields
new_extpost.save()
for tag in post2.tags.all():
new_extpost.tags.add(tag.name) # name is primary key
new_extpost.save()
post2.delete()
# Get rid of author and tags in post1 and post2
orm['post1.author'].objects.all().delete()
orm['post1.tag'].objects.all().delete()
orm['post2.author'].objects.all().delete()
orm['post2.tag'].objects.all().delete()
def backwards(self, orm):
raise RuntimeError("No backwards.")
これらの移行を適用します。
$./manage.py migrate
次に、冗長になった部分を post1/models.py と post2/models.py から削除し、schemamigrations を作成してテーブルを新しい状態に更新します。
$./manage.py schemamigration post1 --auto
$./manage.py schemamigration post2 --auto
$./manage.py migrate
そして、それだけです!すべてが機能し、モデルがリファクタリングされたことを願っています。