Q: How can I use a SQL nextval function in a Django insert - Custom Model Field? I'm trying to write a model field that when called on a model insert will issue a nextval("my_seq") such that the resulting query that django issues is something like:
INSERT INTO app_table (a, b) VALUES ("something", nextval("my_seq"));

So far I've come up with a field that I override get_db_prep_value and make it return the nextval string when the object is being inserted, but it gets quoted. 
How do I get around the value being quoted?
This is what I have for the field:
class MyField(models.Field):
    def __init__(self, *args, **kwargs):
        self.obj = None
        super(MyField, self).__init__(*args, **kwargs
    def contribute_to_class(self, cls, name):
        super(myField, self).contribute_to_class(cls, name)
        setattr(cls, self.name, self)
    def __get__(self, obj, tp = None):
        if obj is None:
            raise AttributeError('Can only be accessed via instance')
        self.obj = obj
        return obj.__dict__[self.name]
    def __set__(self, obj, value):
        obj.__dict__[self.value] = self.to_python(value)
    def db_type(self, connection):
        return 'varchar(25)'
    def get_db_prep_value(self, value, connection, prepared = False):
        value = super(MyField, self).get_db_prep_value(value, connection, prepared)

        if self.obj is not None and self.obj.id is None:
            if self.obj.flag:
                value = '( "XXX" || nextval("my_seq") )'
        return value
    def to_python(self, value):
        if isinstance(value, six.string_types) or value is None:
            return value
        return smart_text(value)
    def get_prep_value(self, value):
        return self.to_python(value)


A: I am not sure if what you are doing is the best Django practice but here is how you can use nextval.
Add to your model
    def get_placeholder(self, value, connection):
            return 'nextval("%s")'

Also consider using SubfieldBase
in Django 1.5+
class MyField(models.Field, metaclass=models.SubfieldBase):

see The SubfieldBase metaclass
in Django < 1.5
__metaclass__ = models.SubfieldBase

see Writing a custom field in Django
