Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude field in Django models.Model.save()

I have the following model, which when saved calculates hash_id field based on the pk:

class MyTable(models.Model):
    something = models.CharField(max_length=255)
    reported = models.IntegerField(default=0, blank=True)
    hash_id = models.CharField(max_length=32, db_index=True, unique=True, blank=True)

    def save(self, *a, **kw):
        super().save(*a, **kw)
        self.hash_id = hash_fn(self.pk)
        super().save(*a, **kw)

In one of my views I have the following lines, which are supposed to increment reported field by 1, however reported is incremented by 2, because of the overridden save method:

my_table_ins.reported = F('reported') + 1
my_table_ins.save()

Ideally I would like something among the lines:

    def save(self, *a, **kw):
        super().save(*a, exclude=['reported'], **kw)
        self.hash_id = hash_fn(self.pk)
        super().save(*a, **kw)
like image 455
NarūnasK Avatar asked Jan 03 '23 10:01

NarūnasK


1 Answers

Adding to @martin-castro-alvarez answer, if you want to update all fields except for a few, you can do this:

fields_to_exclude = {'reported','another_field'}
# automatically populate a list with all fields, except the ones you want to exclude
fields_to_update = [f.name for f in MyTable._meta.get_fields() if f.name not in fields_to_exclude and not f.auto_created]
my_table_ins.save(update_fields=fields_to_update)

this will work in Django>=1.8. In older versions you can use model._meta.get_all_field_names()

like image 68
samad montazeri Avatar answered Jan 05 '23 22:01

samad montazeri