zenwalker
7/30/2017 - 11:27 AM

next_pk.py

from django.db import connection
from django.utils.deconstruct import deconstructible


@deconstructible
class NextPk:
    def __init__(self, table_name):
        self.table_name = table_name

    def __call__(self):
        with connection.cursor() as cursor:
            cursor.execute("select nextval('{}_id_seq')".format(self.table_name))
            next_id_row = cursor.fetchone()
            return next_id_row[0]

    def __eq__(self, other):
        return self.table_name == other.table_name