Django: Queryset.clear() method on OneToOneField()
I need to avoid cascading deletes on a foreign key, but it's a OneToOneField(), like:
class MyModel(models.Model):
def delete(self):
self.mysubmodel.clear() # Breaks because self.cartitem is not a QuerySet.
super(MyModel, self).delete()
class MySubModel(models.Model):
mymodel = models.OneToOneField(MyModel)
This version would work (but I can't use this version):
class MyModel(models.Model):
def delete(self):
self.mysub开发者_开发百科model_set.clear() # Works because self.mysubmodel_set is a QuerySet.
super(MyModel, self).delete()
class MySubModel(models.Model):
mymodel = models.ForeignKey(MyModel)
Setting the field to None in the delete method should work:
self.mysubmodel = None
What is expected behavior here? I mean, o2o does not allow null values?
精彩评论