1👍
✅
new_farm
hasn’t been saved yet, so it doesn’t have a primary key. Other items can’t be related to it until it does have one – what would they put in their FK field?
You actually don’t need the commit=False
at all. Just save it there at the start of the is_valid
block; there’s no need to save it again at the end, because you haven’t changed the Farm object, you’ve only changed the Files.
(I wonder if the FK relationship is the right one here. That’s a one-to-many, so you get existing Files and change whatever Farm they are pointing at to the new one. Is that really what you want to do? I suspect a many-to-many relationship would be better, so files can point to multiple farms.)
Source:stackexchange.com