[Answered ]-IntegrityError Django ForeignKey sets to none

1👍

✅

If you are not passing "author" as a field while saving BugTracker model, you’ll get ({bug.author}} as None, because you have set "null=True" in the "author" field declaration – This means you can save BugTracker model without passing an ‘author’ field and it defaults to null/None.

If you want to set/add an author object to bug_tracker_object, you can do the following. (Note: bug_tracker_object is the BugTracker object that you’ve populated using fields such as project_number, assignee, priority etc.)

author_object = Author.objects.get(id=<your_author_id>)
bug_tracker_object.author_object = author_object 
bug_tracker_object.save()

Leave a comment