新手管理员中的一对多关系问题

huangapple go评论143阅读模式
英文:

Newbie one-to-many relationship problem in admin

问题

我正在编写一个简单的应用程序,其中存在一对多的关系,类似于父母与子女之间的关系,一个父母可能有多个子女。

在这种情况下,我为子女提供了一个外键,指向父母。

父对象不提到子女,因为关系是另一个方向的。

这没问题,但在管理系统中,显示父对象的页面要求填写子字段。但有些情况下,父对象没有子女。

我该如何告诉管理系统子字段可以留空?

感谢任何帮助。

英文:

I am writing a simple application where there is a one to many relationship similar to that of Parent to Child, where one parent may have many children.

In this case I give the child a foreign key referring to to the parent.

The Parent object doesn't mention children because the relationship is in the other direction.

That is fine, but in the admin system the page showing the parent insists that the child fieldd be filled in. There are however cases where a parent has no children.

How do I tell the admin system that the child field may be left blank?

Thanks for any help.

答案1

得分: 1

你可以简单地将这一行放入子模型的“model”属性中:parent = models.ForeignKey(Parent, on_delete=models.CASCADE, blank=True)。不过,为了将来的参考,请确保包含代码片段,因为你可能需要修改这行代码以适应你的代码。

英文:

You can simply put this line in for the model attribute in the Child Model parent = models.ForeignKey(Parent, on_delete=models.CASCADE, blank=True) Although for future reference make sure to include code snippets, because you may need to modify this line to fit your code.

huangapple
  • 本文由 发表于 2023年7月3日 03:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定