I have a Django model against a postgresql database table where I want Django to not insert field_3 during object creation as its a timestamp field that DB is supposed to fill in.
class AbcModel:
model_id = models.AutoField(primary_key=True)
field_1 = models.BigIntegerField()
field_2 = models.TextField()
field_3 = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'abc_table'
The corresponding table schema
abc_table
- model_id integer not null default
nextval('command_running_command_id_seq'::regclass) - field_1 integer not null
- field_2 text not null
- field_3 timestamp without time zone default now()
What I tried - I read that one should call Model.save(update_fields=[list of fields to be updated]).
class AbcModel(models.Model):
def save(self, *args, **kwargs):
u_fields = ['field_1', 'field_2']
super(CommandRunning, self).save(update_fields=u_fields)
But I get an Exception
Cannot force an update in save() with no primary key.
If I include model_id in update_fields, I get following exception
The following fields do not exist in this model or are m2m fields: model_id
What am I missing here?
field_3 = models.DateTimeField(auto_now_add=True)
to add the date and not have to worry about finding a workaround? Or am I missing something? – Commando