Answer the question
In order to leave comments, you need to log in
How to properly remove a ForeignKey link?
Greetings!
There is a model for articles of the following form:
class Article(models.Model):
slug = models.SlugField(max_length=150, unique=True)
title = models.CharField(max_length=150, db_index=True)
description = models.TextField(blank=True, db_index=True)
parent = models.ForeignKey('self', on_delete=models.CASCADE)
{
"articles": [
{
"slug": "page-title-1",
"title": "Page title 1",
"description": "Page description 1",
"parent_id": null
},
{
"slug": "page-title-2",
"title": "Page title 2",
"description": "Page description 2",
"parent_id": null
}
]
}
{
"article": {
"slug": "page-title-2",
"title": "Page title 2",
"description": "Page description 2",
"parent_id": 1
}
}
parent_id
contains the value 1. I try to reverse the manipulation (disconnect the link between the articles), send:{
"article": {
"slug": "page-title-2",
"title": "Page title 2",
"description": "Page description 2",
"parent_id": null
}
}
{
"parent_id": [
"This field may not be null."
]
}
Answer the question
In order to leave comments, you need to log in
maybe you should allow parent to be null ?
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question