pbm
pbm

Reputation: 378

Django - null ForeignKey

I have class SubForum with ForeignKey to self - parent:

class Forum(models.Model):
    name = models.CharField(max_length=200)
    url = models.URLField()

class SubForum(models.Model):
    name = models.CharField(max_length=200)
    orginal_id = models.IntegerField()

    forum = models.ForeignKey('Forum')
    parent = models.ForeignKey('self', null=True, blank=True)

I want to allow for null and blank enteries - I saw examples that this is a proper way to do that.

In sql view everything is ok:

BEGIN;CREATE TABLE "main_forum" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(200) NOT NULL,
    "url" varchar(200) NOT NULL
)
;
CREATE TABLE "main_subforum" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(200) NOT NULL,
    "orginal_id" integer NOT NULL,
    "forum_id" integer NOT NULL REFERENCES "main_forum" ("id"),
    "parent_id" integer
)
;COMMIT;

In parent_id field there is no NOT NULL, but when I want to add new SubForum using admin panel without setting parent i get error:

Cannot assign None: "SubForum.parent" does not allow null values.

What's wrong?

Upvotes: 9

Views: 6668

Answers (1)

pbm
pbm

Reputation: 378

I made some changes, reverted it back and now everything is working fine... and I don't see any difference with code that I posted here...

Should I delete question?

Upvotes: 2

Related Questions