开发者

Django model foreign key removal

开发者 https://www.devze.com 2023-04-02 17:24 出处:网络
I have following model: class Client(models.Model): user = models.OneToOneField(DjangoUser, unique=True)

I have following model:

class Client(models.Model):
    user = models.OneToOneField(DjangoUser, unique=True)
    address = models.ForeignKey(Address,blank=True)

class Address(models.Model):
    (...)

Then I do:

client=Client()
client.address=address #any Address instance
c开发者_运维问答lient.save()

And now: how can I remove foreign association key from client?

client.address=None 

seem not to work.


To be able to null out a foreign key, it's not enough to set in blank. You must also specify that null=True is also set on the field. See The Difference Between Blank and Null.


Your current models setup does not allow null=True, thus you cannot set it to None.


address = models.ForeignKey(Address,blank=True, null=True)

the key is null=True as well as blank=True

also, make sure to syncdb etc

0

精彩评论

暂无评论...
验证码 换一张
取 消