bartek
bartek

Reputation: 3021

Django model foreign key removal

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
client.save()

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

client.address=None 

seem not to work.

Upvotes: 0

Views: 3334

Answers (3)

Elf Sternberg
Elf Sternberg

Reputation: 16381

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.

Upvotes: 1

Francis Yaconiello
Francis Yaconiello

Reputation: 10939

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

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

also, make sure to syncdb etc

Upvotes: 1

Uku Loskit
Uku Loskit

Reputation: 42050

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

Upvotes: 1

Related Questions