Lar*_*aro 6 python django generic-foreign-key django-migrations
我想为Comment具有GenericForeignKey关系的model()创建数据迁移.我的模型是根据django文档为contenttypes制作的.
楷模:
...
class NiceMeme(models.Model):
"""
Example model.
"""
name = models.CharField(max_length=140)
image = models.ImageField(upload_to=get_path_to_store_nice_meme_images)
class Comment(models.Model):
"""
Model to add comments to any other (non abstract) model.
"""
...
user = models.ForeignKey(ExtendedUser)
content = models.CharField(max_length=140)
content_type = models.ForeignKey(ContentType)
object_pk = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_pk')
Run Code Online (Sandbox Code Playgroud)
数据迁移:
...
def create_comment(apps, schema_editor):
...
nice_meme = NiceMeme.objects.create(name='Nice nice meme')
Comment.objects.create(
user=user,
content='Gott ist tot',
poster_username='Friedrich',
content_object=nice_meme
)
...
operations = [
migrations.RunPython(create_comment)
]
Run Code Online (Sandbox Code Playgroud)
当我跑步时,./manage.py migrate我得到:
TypeError: 'content_object' is an invalid keyword argument for this function
我必须说我create_comment在视图中使用了相同的代码并且运行良好.
我正在使用django 1.7.7.我不是在南方.
编辑: 我试过了王尚的回答.
Comment.objects.create(
user=user,
content='Gott ist tot',
poster_username='Friedrich',
content_type=ContentType.objects.get_for_model(nice_meme),
object_pk=nice_meme.id
)
Run Code Online (Sandbox Code Playgroud)
不工作:
ValueError: Cannot assign "<ContentType: nice meme>": "Comment.content_type" must be a "ContentType" instance.
Run Code Online (Sandbox Code Playgroud)
Raf*_*ger 11
正如我所说,我遇到了同样的问题,一位同事帮我解释了这个问题:
你确实需要设置content_type和object_pk+ Shang Wang指出,但ContentType必须使用apps.get_model而不是直接导入它.
所以使用这个:
ContentType = apps.get_model('contenttypes', 'ContentType')
在您的迁移方法中,所有应该工作:)
def create_comment(apps, schema_editor):
...
ContentType = apps.get_model('contenttypes', 'ContentType')
nice_meme = NiceMeme.objects.create(name='Nice nice meme')
Comment.objects.create(
user=user,
content='Gott ist tot',
poster_username='Friedrich',
content_type=ContentType.objects.get_for_model(nice_meme),
object_pk=nice_meme.id
)
Run Code Online (Sandbox Code Playgroud)
请尝试这个:
from django.contrib.contenttypes.models import ContentType
nice_meme = NiceMeme.objects.create(name='Nice nice meme')
Comment.objects.create(user=user,
content='Gott ist tot',
poster_username='Friedrich',
content_type=ContentType.objects.get_for_model(nice_meme),
object_pk=nice_meme.id)
Run Code Online (Sandbox Code Playgroud)
我认为问题在于您的content_object字段只是Comment模型对象快速访问外键的简单方法,例如:
obj = some_comment.content_object
Run Code Online (Sandbox Code Playgroud)
它不是一个实际的字段,而是 2 个字段的组合,因此您不能直接为其分配一个NiceMeme对象。
编辑:
听起来您正在使用 South,这里描述了一个问题。该解决方案在另一个 SO线程中得到了回答。听起来解决方案是冻结与迁移相关的任何模型:
python manage.py schemamigration --auto yourapp --freeze contenttypes
Run Code Online (Sandbox Code Playgroud)
您可能需要冻结更多应用程序:
python manage.py schemamigration --auto yourapp --freeze contenttypes --freeze someotherapp ...
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这个问题,因此请阅读原始帖子以了解更多详细信息,希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
1556 次 |
| 最近记录: |