我想进行数据迁移以便在数据库中添加用户阅读帖子。有这样的代码:为什么GenericRelation字段在Data Migrations中不起作用(Django)
def user_read_posts(apps, schema_editor):
User = apps.get_model("main", "User")
Post = apps.get_model("main", "Post")
Comment = apps.get_model("comments", "Comment")
comments = Comment.objects.all()
for comment in comments:
print (comment.content_object.__class__.__name__)
if isinstance(comment.content_object, Post):
comment.user.read_posts.add(comment.content_object)
class Migration(migrations.Migration):
dependencies = [
('main', '0039_auto_20160314_0906'),
]
operations = [
migrations.RunPython(user_read_posts),
]
并且符合print (comment.content_object.__class__.__name__)
Django的提高错误: AttributeError: 'Comment' object has no attribute 'content_object'
评价模型:
class GuidaComment(GenericRelationModel):
user = models.ForeignKey(GuidaUser)
text = models.TextField()
那么,应该怎么办?
谢谢。
你能给类GenericRelationModel'的'属性? – ruddra
我使用django generic_helpers app.As在文档中:'from generic_helpers.models import GenericRelationModel'这是一个模型模块https://github.com/marazmiki/django-generic-helpers/blob/master/generic_helpers/models.py –
考虑使用这个:https://docs.djangoproject.com/en/1.9/ref/contrib/contenttypes/,因为我可以看到这个软件包已经有近1年没有更新过了。 – ruddra