0

Could you please advice how to set foreign-key to "NULL"? I the following example I want to override delete method in order to not delete images but instead set their FK to "NULL" in order to recover and reconnect them with the parent objects later on.


def delete(self, using=None, keep_parents=False):        
    self.foreighnkey = None # foreighnkey  is the fk field's name
    self.save()

# https://stackoverflow.com/questions/4446652/django-how-to-set-a-field-to-null
# doesn't work this way

P.S. Parent objects will stay intact, so that use their delete methods is not an option

Zoe
  • 27,060
  • 21
  • 118
  • 148
Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27

1 Answers1

0

It is possible in this case to get Primary model instance and then use it's related manager to call

Primary_model_instance.secondarymodel_set.remove(self)

# or alternativelly
 self.foreighnkey.currentmodel_set.remove(self)

Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27