How to deal with an auto-incrementing primary key
I am using Django 1.2.4 with a Postgresql 8.4 backend. Before creating my models in Django, I began with a existing Postgresql database with tables for which I had defined integer primary keys that used an autoincrementing sequence on table inserts. When I created my Django models, I defined the primary keys as IntegerFields with primaryKey set to true using the same primary key field names as in the pre- existing database tables. This autoincremented variable set in postgresql is visible in the Django admin as "Sample_ID", with an empty form field. Because the user can't possibly know what the next value in the sequence should be, I'm wondering how to deal with the field, as it is Not NULL, and any attempt to submit without a value returns an error. Is there perhaps a way to make the field not visible in admin, but have the field automatically filled in once the rest of the form fields are submitted? -- 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.
Re: How to deal with an auto-incrementing primary key
http://docs.djangoproject.com/en/1.3/ref/contrib/admin/ Check out 'fields' and 'exclude.' -- 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.
Re: How to deal with an auto-incrementing primary key
Using exclude works to eliminate this sample_id field from the form, but I get a 'Null value in column "sample_id" violates not-null constraint' If one inserts a record directly through Postgresql command line, the sample_id field gets incremented automatically with its sequence, but it does not do so when adding a new record with the Django form... On May 10, 10:19 am, Shawn Milochik wrote: > http://docs.djangoproject.com/en/1.3/ref/contrib/admin/ > > Check out 'fields' and 'exclude.' -- 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.
Re: How to deal with an auto-incrementing primary key
What's your model look like? Did you add the foreign_key = True kwarg to the field in question? -- 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.
Re: How to deal with an auto-incrementing primary key
Thanks for helping on this Shawn... My model definition looks like this: class Sample(models.Model): met_type = models.ForeignKey(MetType, verbose_name='Meteorite Type') sample_name = models.CharField(max_length=100, verbose_name='Sample Name') sample_id = models.IntegerField(primary_key=True, verbose_name='Sample ID') location = models.CharField(max_length=100,blank=True,null=True, verbose_name='Location') fall_date = models.CharField(max_length=20,blank=True,null=True, verbose_name='Fall Date') objects=models.Manager() class Meta: db_table = u'sample' ordering = ['sample_name'] def __unicode__(self): #return unicode(self.sample_name) return self.sample_name On May 10, 11:10 am, Shawn Milochik wrote: > What's your model look like? > > Did you add the foreign_key = True kwarg to the field in question? -- 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.
Re: How to deal with an auto-incrementing primary key
I think the problem is that you used IntegerField instead of AutoField. http://docs.djangoproject.com/en/1.3/ref/models/fields/#autofield However, why even bother? Why not get rid of that field and use the built-in id field that you're going to get from a Django model? There's no benefit at all -- you're just duplicating what would happen automatically. Plus, it's causing you all this trouble and wasting your time. Shawn -- 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.
Re: How to deal with an auto-incrementing primary key
I will try just removing it from the model. Does this mean I should remove the sample_id variable from the Postgres database as well. Will running syncdb create the new id field for that table? On May 10, 10:33 am, wilbur wrote: > Using exclude works to eliminate this sample_id field from the form, > but I get a > > 'Null value in column "sample_id" violates not-null constraint' > > If one inserts a record directly through Postgresql command line, the > sample_id field gets incremented automatically with its sequence, but > it does not do so when adding a new record with the Django form... > > On May 10, 10:19 am, Shawn Milochik wrote: > > >http://docs.djangoproject.com/en/1.3/ref/contrib/admin/ > > > Check out 'fields' and 'exclude.' -- 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.
Re: How to deal with an auto-incrementing primary key
If you don't mind losing all your data you can destroy the database then do syncdb. If you re-run syncdb without re-creating the database then it will do nothing for existing tables. You can use South[1] if you need to keep your data intact. [1] http://south.aeracode.org/ -- 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.
Re: How to deal with an auto-incrementing primary key
I definitely need to keep the database, and have South installed, but should I get rid of the original primary key and the sequence it depends on? Will south add in the new ID field? On May 10, 11:48 am, Shawn Milochik wrote: > If you don't mind losing all your data you can destroy the database then > do syncdb. > > If you re-run syncdb without re-creating the database then it will do > nothing for existing tables. > > You can use South[1] if you need to keep your data intact. > > [1]http://south.aeracode.org/ -- 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.
Re: How to deal with an auto-incrementing primary key
> > Will south add in the new ID field? I have never come across a situation where South was not able to change something in a table for me, so far. Try it please. On Tue, May 10, 2011 at 1:52 PM, wilbur wrote: > I definitely need to keep the database, and have South installed, but > should I get rid of the original primary key and the sequence it > depends on? Will south add in the new ID field? > > On May 10, 11:48 am, Shawn Milochik wrote: > > If you don't mind losing all your data you can destroy the database then > > do syncdb. > > > > If you re-run syncdb without re-creating the database then it will do > > nothing for existing tables. > > > > You can use South[1] if you need to keep your data intact. > > > > [1]http://south.aeracode.org/ > > -- > 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. > > -- AJ -- 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.
Re: How to deal with an auto-incrementing primary key
If you are absolutely sure the field is filled in (the sequence is defined) I guess you can allow it to be null in the model definition. On Tue, May 10, 2011 at 7:33 PM, wilbur wrote: > Using exclude works to eliminate this sample_id field from the form, > but I get a > > 'Null value in column "sample_id" violates not-null constraint' > > If one inserts a record directly through Postgresql command line, the > sample_id field gets incremented automatically with its sequence, but > it does not do so when adding a new record with the Django form... > > On May 10, 10:19 am, Shawn Milochik wrote: >> http://docs.djangoproject.com/en/1.3/ref/contrib/admin/ >> >> Check out 'fields' and 'exclude.' > > -- > 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. > > -- 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.