管理界面中仍需要使用 Through 和 Blank=True 的 ManyToManyField

发布于 2024-10-07 08:34:45 字数 543 浏览 0 评论 0原文

我的模型(部分代码):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-10-14 08:34:45

我通过改进 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.

凉栀 2024-10-14 08:34:45

这也发生在我身上。

你具体是怎么解决的?

以下修改似乎可以解决问题:
但不确定它在数据库级别产生什么影响。

class Value(models.Model):
    label = models.ForeignKey(Label, blank=True, null=True)

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.

class Value(models.Model):
    label = models.ForeignKey(Label, blank=True, null=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文