I would like to replace null value in my django objects with the default value :
Example :
class Buy(models.Model):
created = models.DateTimeField(null=True, blank=True, editable=False,default=timezone.now)
modified = models.DateTimeField(null=True, blank=True,default=timezone.now)
name = models.CharField(null=True,blank=True, max_length=200,default='')
Here is my script :
for buy in Buy.objects.all():
for f in Buy._meta.get_fields():
field = eval("buy." + f.name+" is None")
if field:
field = Buy._meta.get_field(f.name).get_default()
buy.save()
but when i make a json export with dumpdata i still have
"name" : null,
in my json for a lot of objects instead of
"name": ""
.
I don't understand my script for migrating the null value of my object to "" seems to be correct.
Regards
if fieldonly runs if the field is not null, did you meanif not field?fieldis a local variable. Changing it will have no effect. Why not usegetattrandsetattrrather thaneval?