Search code examples
djangoadminone-to-many

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.


Solution

  • 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.