[Answer]-Why can't I create a ForeignKey field to a child class in django?

1👍

I have Items (A), some of which are Folders (B). I want both classes
to have a reference to at most one Folder

It doesn’t make much sense(to me) what you’re trying to do but this can be achieved
as follows:

class Item(models.Model):
    # some fields
    is_folder = models.BooleanField(default=False)
    some_other_folder = models.ForeignKey('self', null=True, blank=True)

And then check with python code that if is_folder==False, that some_other_folder
is not None(null).

So actually you don’t need 2 models.

Leave a comment