Hi, I have a Django newbie question. My goal is to auto increment primary key with non Django standard column name. We are converting from existing database and primary key schema is "tablename_key" and not "id". I googled it and end up reaching to this ticket: https://code.djangoproject.com/ticket/13295 So I understand that there is work in progress but I wanted find work around..
1. My first try was to let postgres handle it. my postgreSQL table looks like this: CREATE TABLE poll ( poll_key integer NOT NULL DEFAULT nextval('poll_key_seq'::regclass), poll_question character varying(200) NOT NULL, poll_pub_date timestamp with time zone NOT NULL, CONSTRAINT poll_pkey PRIMARY KEY (poll_key) ) My model look like this: class Poll(models.Model): poll_key = models.IntegerField(primary_key=True) poll_question = models.CharField(max_length=200, default='') poll_pub_date = models.DateTimeField('date published', default=datetime.date.today()) class Meta: db_table = u'poll' I was hoping that with this, I could p = Poll(poll_question="Question 1?") p.save() but this fails because Django is actually sending the following statement: INSERT INTO "poll" ("poll_key", "poll_question", "poll_pub_date") VALUES (NULL, 'Question 1?', NULL) 2. My Second attempt is then to add default to model Created a function to return sequence value from django.db import connection def c_get_next_key(seq_name): """ return next value of sequence """ c = connection.cursor() c.execute("SELECT nextval('%s')" % seq_name) row = c.fetchone() return int(row[0]) Calling like below works just fine. Everytime I call it, I get new number. c_get_next_key('poll_key_seq') Then I modify Poll_key in Poll model as follows: Poll_key = models.IntegerField(primary_key=True, default=c_get_next_key('poll_key_seq')) I went to Django Shell and created first record. p1 = Poll(poll_question="P1") p1.poll_key # this will return let's say 37 p1.save() # saves just fine. # instantiating new object p2 = Poll(poll_question="P2") p2.poll_key # this also return 37. I know I must be doing something wrong... but could you pint me to right direction? I am expecting p2.poll_key to return 38. Thank you very much for your help in advance. Naoko -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.