﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29672	Returns an empty model field that is filled with a trigger in the database.	Vaskevich Aleksander	nobody	"my model

{{{
blank_and_null = {'blank': True, 'null': True}

class Message(....):
    ....
    chat_local_id = models.BigIntegerField(
        verbose_name='local id message for chat',
        **blank_and_null)
    ....
}}}

I transferred some logic to the trigger database.
Here is my trigger

{{{
CREATE OR REPLACE FUNCTION public.inc_chat_local_id()
     RETURNS trigger
     LANGUAGE 'plpgsql' AS
$BODY$
DECLARE
	new_count_message INTEGER;
BEGIN
   	UPDATE v1_chat_chat SET count_message = count_message + 1
 		WHERE id = NEW.chat_id;

	SELECT v1_chat_chat.count_message INTO new_count_message FROM v1_chat_chat
		WHERE id = NEW.chat_id;

	NEW.chat_local_id = new_count_message;

 	RETURN NEW;
END;
$BODY$;

DROP TRIGGER IF EXISTS generate_chat_local_id on v1_chat_message;

CREATE TRIGGER generate_chat_local_id
  BEFORE INSERT
  ON v1_chat_message
  FOR EACH ROW
  EXECUTE PROCEDURE inc_chat_local_id();
}}}

It works as I expect
before insertion requests, it sets the value of chat_local_id
when I execute the insert query through django
chat_local_id empty field
when I re-query this entry, the chat_local_id is set to

my Python version: 3.6
my database: Postgres
"	Bug	closed	Uncategorized	2.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
