0

I have defined class Directory as follows:

class Directory(models.Model):
   dir_name = models.CharField(max_length=100)
   parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True)

   def __str__(self):
      return self.dir_name

When I enter a specific UI for the first time, the program will call a function in views.py to check if any directory exists, otherwise create the root directory:

root, created = Directory.objects.get_or_create(dir_name='media/', **insert null to foreign key**)

The reason of the above is that the root directory has no parent. How can I achieve this in django syntax? (I've tried something like parent='' and parent=null, but none of them worked). Thanks a lot in advance!

1
  • 1
    Have you tried parent=None? Commented Jun 1, 2021 at 2:13

1 Answer 1

2

As bdbd said, try passing parent=None, or not to pass parent at all

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.