开发者

Django: Queryset.clear() method on OneToOneField()

开发者 https://www.devze.com 2022-12-18 07:13 出处:网络
I need to avoid cascading deletes on a foreign key, but it\'s a OneToOneField(), like: class MyModel(models.Model):

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?

0

精彩评论

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