管理界面中仍需要使用 Through 和 Blank=True 的 ManyToManyField
我的模型(部分代码):
class Observation(models.Model):
date = models.DateField()
geom = models.PointField()
values = models.ManyToManyField(Label, through='Value', null=True, blank=True)
objects = models.GeoManager()
class Value(models.Model):
observation = models.ForeignKey(Observation)
label = models.ForeignKey(Label)
value = models.CharField(max_length=100)
objects = models.GeoManager()
当我在管理界面中管理观察对象时,它仍然表示每个观察至少需要一个值。
我做错了什么吗,这是一个错误,还是我应该编写一个派生的 Admin 类来解决这个问题?
My model (partial code):
class Observation(models.Model):
date = models.DateField()
geom = models.PointField()
values = models.ManyToManyField(Label, through='Value', null=True, blank=True)
objects = models.GeoManager()
class Value(models.Model):
observation = models.ForeignKey(Observation)
label = models.ForeignKey(Label)
value = models.CharField(max_length=100)
objects = models.GeoManager()
When I manage an Observation object in the admin interface, it still says at least one value per observation is required.
Am I doing something wrong, is this a bug, or should I write a derived Admin class to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过改进 ERM 解决了这个问题。 Observation 中的字段值已过时,因为您从 Value 中的foreignkey 关系中获取 value_set。
仍然是一个奇怪的副作用,但由于没有回复,我会认为它不经常发生。
I solved this by improving my ERM. The field values in Observation is obsolete, since you get a value_set from the ForeignKey relation in Value.
Still a weird side effect, but since there were no replies I'll consider it something that doesn't occur often.
这也发生在我身上。
你具体是怎么解决的?
以下修改似乎可以解决问题:
但不确定它在数据库级别产生什么影响。
This is happened to me too.
How exactly you got it resolved?
Following modification seems to do the trick:
But not sure, what effect it's having at DB level.