Django inlinemodeladmin 验证 - 但具有通用关系
我以前有这样的模型:
class AssemblyAnnotation(models.Model):
assembly = models.ForeignKey(Assembly)
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if not self.type.can_annotate_aliases and self.assembly.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
效果是,新的 AssemblyAnnotation(通过内联附加)只能具有其类型属性的值的子集,具体取决于父程序集。
这效果很好。
现在,是时候将这些注释应用到略有不同的其他对象了:
class ObjectAnnotation(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if self.content_type == ContentType.objects.get_for_model(Assembly):
if not self.type.can_annotate_aliases and self.content_object.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
如您所见,我希望应用相同的规则。然而,有一个问题。我现在使用的 GenericInline 在 clean() 方法运行之前不会设置 self.content_type 。
有什么办法解决这个问题吗?我这样做错了吗?
感谢您的帮助。
I previously had models like this:
class AssemblyAnnotation(models.Model):
assembly = models.ForeignKey(Assembly)
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if not self.type.can_annotate_aliases and self.assembly.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
The effect was, a new AssemblyAnnotation (attached via an inline) could only have a subset of values for it's type attribute, depending on the parent Assembly.
This worked great.
Now, it has come time to apply these annotations to other objects that are slightly different:
class ObjectAnnotation(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
type = models.ForeignKey(AssemblyAnnotationType)
...
def clean(self):
from django.core.exceptions import ValidationError
if self.content_type == ContentType.objects.get_for_model(Assembly):
if not self.type.can_annotate_aliases and self.content_object.alias_of_id is not None:
raise ValidationError('The selected annotation type cannot be applied to this assembly.')
As you can see, I want the same rules applied. However, there is a problem. The GenericInline that I am now using does not set self.content_type before my clean() method is run.
Is there any way around this? Am I doing this wrong?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
右侧不会返回一个列表吗
如果 self.content_type == ContentType.objects.get_for_model(Assembly):
?我认为您需要执行
if self.content_type in ContentType.objects.get_for_model(Assembly):
Wouldn't the right hand side return a list in
if self.content_type == ContentType.objects.get_for_model(Assembly):
?I would think you would need to do
if self.content_type in ContentType.objects.get_for_model(Assembly):
我用的和你一样,效果也符合预期。这是我的代码:
在模型中:
在管理中:
经过一些测试,我发现
content_type
和object_id
(在我的例子中是flag_pk
) 字段在 clean() 调用之前设置,但 GenericForeignKey(在我的例子中为flag
)没有设置。I'm using the same as you and it works as espected. Here's my code:
In the Models:
And in the Admin:
After some tests, I found that the
content_type
andobject_id
(flag_pk
in my case) fields are set before the clean() call, but theGenericForeignKey
(flag
in my case) doesn't.