Malcolm Tredinnick <[EMAIL PROTECTED]> writes: > On Sat, 2007-02-10 at 03:34 -0500, David Abrahams wrote: >> >> In my attempt to use >> >> ForeignKey(Track, null=True) >> >> When I actually tried to use a null Track value, I got: > [...traceback snipped...] >> >> ProgrammingError at /admin/program/session/15/ >> ERROR: invalid input syntax for integer: "" SELECT >> "program_session"."id","program_session"."title","program_session"."short_title","program_session"."description","program_session"."start_id","program_session"."track_id","program_session"."duration" >> FROM "program_session" INNER JOIN "program_timedivision" AS >> "program_session__start" ON "program_session"."start_id" = >> "program_session__start"."id" WHERE ("program_session__start"."id" ILIKE >> '40' AND "program_session"."track_id" = '') >> >> My wild guess here is that this happens because track_id is the primary key >> and thus expects an integer value; had I made the track name its >> primary key it might've worked (?). Anyway, if I want to make that >> change at this point, I don't know how to do so without losing my >> valuable data. Any help you can offer me would be very much >> appreciated. > > Can you give a little more information about how you triggered this > error? It looks like you are doing something in the admin,
I am. I'm saving an existing Session object after setting its Track to the "------" entry in the pop-up used for track selection. I get a similar effect when creating a new Session without setting the Track. > but I am using null=True foreign keys through the admin successfully > without doing anything special, so it's not totally impossible. It > shows up fine in the admin system and the database stores the > reference as a NULL (in my case). And I'm using PostgreSQL, too, so > it's not a db-specific thing that I can see (although I'm using > psycopg1, not psycopg2). > > If you triggered the problem just by trying to add a record in the admin > and leaving the "track" reference field empty, it's going to be a little > hard to debug remotely. My models.py is enclosed. It doesn't have any special dependencies. Would you mind trying to reproduce the problem? > Try creating a smaller project with a trimmed > down model -- doing the standard "remove stuff until it starts working > or you are only left with the ForeignKey" process. I'll see if I can do that. > I can't place what portion of the admin is generating the query you are > seeing, so any information you can provide would be useful. The endlosed models.py ought to be sufficient. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
from django.db import models class Speaker(models.Model): last_name = models.CharField(maxlength=100) first_name = models.CharField(maxlength=100) email = models.EmailField() bio = models.TextField() class Meta: ordering = ('last_name','first_name','email') class Admin: pass # list_display = ('first_name', 'last_name') def __str__(self): return self.first_name + ' ' + self.last_name class Track(models.Model): name = models.CharField(maxlength=100, primary_key=True) description = models.TextField() class Admin: list_display = ('name', 'description') def __str__(self): return self.name class TimeDivision(models.Model): when = models.DateTimeField() starts_break = models.BooleanField() name = models.CharField(maxlength=100) class Admin: pass # list_display = ('name', 'when', 'starts_break') class Meta: ordering = ('when',) def __str__(self): return self.when.strftime('%A') + ' ' + self.name class Session(models.Model): title = models.CharField(maxlength=200) speakers = models.ManyToManyField(Speaker, filter_interface = models.HORIZONTAL, related_name='Sessions') short_title = models.CharField(maxlength=50,blank=True) description = models.TextField() start = models.ForeignKey(TimeDivision) track = models.ForeignKey(Track,blank=True,null=True) duration = models.TimeField() def __str__(self): return ', '.join([s.last_name for s in self.speakers.all()]) \ + ': ' + (self.short_title or self.title) class Admin: pass class Meta: # These constraints are imperfect but should catch many common errors unique_together = ( ('start','track'), # only one session may start in a given # track at a given time # ('speakers', 'start'), # a given speaker can only start one # session at a time ('title',), # no two sessions can have the same title )
-- Dave Abrahams Boost Consulting www.boost-consulting.com