0

i have an object say it as productObj. when I am iterating through this object if a particular condition is met I have to delete that value from that object.

something like

for data in productObj:
    if data.count > 0:
        data.delete()

if the count is greater than zero the data value should be deleted and the productObj should be updated.

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
Sakeer
  • 1,885
  • 3
  • 24
  • 43

1 Answers1

1

Just working off your own code, then

if data.count > 0:
    data.count = None
#continue with some stuff and lastly call .save()
productObj.save()

should suffice.

Calling .delete() will, in Django, remove the entire object.

However if you want to delete the attribute of that said object, you're gonna have a bad time, especially when you're working of an object that inherits from models.Model but let's go ahead and do it anyways

del productObj.count #from the object

and if you want it stripped from the model you would have to delete it from the models _meta fields which would be the property -> Model._meta.fields

Daniel Roseman explains it brilliantly here

Community
  • 1
  • 1
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92