标签: django-model-utils

Django“ValueError:无法批量创建多表继承模型”

问题

我正在使用 django-model-utils InheritanceManager。我有一个超级通知,我用它来创建多个通知子类,如(models.Model)类PostNotification(Notification)CommentNotification(Notification)等,并试图运行时CommentNotification.objects.bulk_create(list_of_comment_notification_objects),我得到以下回溯:

File "/home/me/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 429, in bulk_create
    raise ValueError("Can't bulk create a multi-table inherited model")
ValueError: Can't bulk create a multi-table inherited model
Run Code Online (Sandbox Code Playgroud)

在检查 query.py 文件时,我们发现这会导致错误:

for parent in self.model._meta.get_parent_list():
      if parent._meta.concrete_model is not self.model._meta.concrete_model:
           raise ValueError("Can't bulk create a multi-table inherited model")
Run Code Online (Sandbox Code Playgroud)

环境 Django Model Utils 版本:3.1.1 Django 版本:1.11.7 Python 版本:2.7.3

例子

PostNotification.objects.bulk_create(
   [PostNotification(related_user=user, post=instance) for user in users]
)
Run Code Online (Sandbox Code Playgroud)

抛出上述异常

我已经尝试过但最初是成功的:

我虽然只是运行: BaseClass.objects.bulk_create(list_of_SubClass_objects)而不是SubClass.objects.bulk_create(list_of_SubClass_objects)会工作并返回子类值的列表,但随后运行SubClass.objects.all()会返回一个空结果。bulk_create() 只会为列表中的每个项目创建一个 …

django django-models django-model-utils

5
推荐指数
1
解决办法
2507
查看次数

没有名为'model_utils'的模块

我正在使用Python 3.4.3和django 1.9.8.

在我的models.py中,我有

来自model_utils.managers import InheritanceManager

但是会发生以下错误:

ImportError:没有名为'model_utils'的模块

python django python-3.x django-model-utils

4
推荐指数
1
解决办法
5658
查看次数

Django-model-utils依子类别筛选

我正在使用django-model-utils继承管理器来查询具有多表继承的数据模型,并且大多数情况下效果很好!不好的是,我想.select_subclasses()过滤特定的子类。例如:

class Salad:
    ...

class Vegetable:
    salad = models.ForeignKey(Salad)
    ...

class Cucumber(Vegetable):
    ...

class Carrot(Vegetable):
    ...
Run Code Online (Sandbox Code Playgroud)

我希望仅能获取Cucumber与特定对象相关的所有对象Salad而没有任何Carrot对象。不幸的是,文档似乎没有对此进行解释。在保存可用于“常规”过滤的任何Vegetable对象时,我最好的选择是typeVegetable我设置的类上创建一个字段吗?提前致谢!

python django inheritance django-model-utils

3
推荐指数
1
解决办法
748
查看次数