0
我有这样一个模型:在运行时python manage.py sql my_test_app
Django MySQL如何停止外键约束?
from django.db import models
class House(models.Model):
address = models.CharField(max_length=200)
price = models.CharField(max_length=200)
class Inhabitant(models.Model):
home = models.ForeignKey(House, blank=True, null=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=200)
age = models.IntegerField()
但是我得到这个结尾:
ALTER TABLE `my_test_app_inhabitant`
ADD CONSTRAINT `house_id_refs_id_7cd928c7`
FOREIGN KEY (`home_id`)
REFERENCES `my_test_app_house` (`id`);
这意味着没有房子我不能添加的居民 - 我怎么能解决这个问题?
你自己在home中定义它 - models.ForeignKey(...):) – favoretti