How to define a Django model field with a PosgreSQL function as the default value

Is it possible to write a Django model with a field using the PostgreSQL function as the default value? Here is a simple example of using txid_current () as the default value:

% psql mydb=# CREATE TABLE test ( label text default '', txid BIGINT DEFAULT txid_current() ); CREATE TABLE mydb=# \d test Table "public.test" Column | Type | Modifiers --------+--------+------------------------ label | text | default ''::text txid | bigint | default txid_current() mydb=# INSERT INTO test (label) VALUES ('mylabel'); INSERT 0 1 mydb=# select * from test; label | txid ---------+-------- mylabel | 192050 (1 row) 

The Django model for this table might look like

 class Test(models.Model): label = TextField('Label', default='') txid = BigIntegerField('txid', default=???) 

Is there a way to specify the database function as the default value, or do I need to add the default value in PostgreSQL as a separate step after running syncdb?

+4
source share
2 answers

You must do this in SQL yourself. Django does not support this. This is a feature of database independence.

0
source

Source: https://habr.com/ru/post/1380783/


All Articles