How can I use a SQL nextval function in a Django insert - Custom Model Field? -


I am trying to write a model field when the call is placed when inserting a model, then nextval (" My_seq ") is something like the queries of the query DNS that arises:

  app_table (A, B) values INSERT ("Some", next ("my_seq"));   

So far I have come up with a field that I override get_db_prep_value and returns it to nextval string when the object Is being inserted, but it gets quoted

How do I get around the quoted value?

I have this area for:

  class MyField (models.Field): def __init __ (self, * Argus, ** kwargs): self.obj = Any Super (MyField, Self) .__ init__ (* args, ** kwargs def contribution _to_class (self, cls, name): Super (MyField, Self) Contribution_sto- Class (CLS, name) setter (CLS, self name, self) DEF __get __ (self, oBJ, TP = none): If OBJE is none: Entitlement Entry (can only be accessed by 'example) self .obj = obj return obj .__ dic T __ [self.name] def __set __ (self, obj, value): obj .__ dict __ [self.value] = self.to_python (value) def db_type (self, connection): 'varchar (25)' def Get_db_prep_value (self, value, connection, ready = false): value = super (MyField, self) .get_db_prep_value (value, connection, ready) if self.obj is not there and nobody has .obj.id none: if Self.obj.flag: value = '("XXX" ||' my_seq ')' return value def to _python (self, value): ifinstance (value, six .string_types) or value none: return value Return smart_text (value) def get_prep_value (self, value): return self.to_python (value)    

I'm not sure what you're doing is the best Django practice, but here's how you can use.

Also consider using SubfieldBase

in Django 1.5 +

  class MyField (models.Field, metaclass = models.SubfieldBase):   

See

in Django & lt; 1.5

  __ metaclass__ = model ShabfieldBase   

View

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -