On Sat, 2006-10-14 at 04:47 -0700, Ewout ter Haar wrote: > I am trying to make use of the django admin, to use with a database > which is normally managed by another (PHP) application. I ran > inspectdb, and after tweaking the models a little bit, it works for > basic fields like InterFields, CharFields etc. > > I would also like to use the ForeignKey features that the admin > offers. But the semantics of the legacy schema is such that if the > field is optional, it has a value -1, like this: the schema I want to > emulate > in models.py is > > CREATE TABLE `prefix_users` ( > `ident` int(10) unsigned NOT NULL auto_increment, > `icon` int(11) NOT NULL default '-1' COMMENT '-> icons.ident', > [...] > > which I do with > > class Users(models.Model): > ident = models.AutoField(primary_key=True) > icon = models.ForeignKey(Icon,blank=True,db_column='icon') > [...] > > This works if I choose a value for the icon field, but this does not > work if I don't want an icon for a user. My legacy schema expects > a value -1 for the icon field in this case, which I do not know how > to reproduce in Django. Any ideas?
The value of Users.icon either has to be a valid primary key value in the table for the Icon model or NULL (of you have null=True in the ForeignKey constructor). This is because a Django ForeignKey field is a true foreign key in the sense that it creates a field containing the primary key of the foreign table *and* it creates a constraint ensuring that the values in that field are valid (i.e. values from the corresponding field in the foreign table). The constraint is significant here. So putting a non-existent non-NULL value in that field means it is no longer a foreign key. If you want to use Django's foreign key functionality, you will need to make sure this field really is a foreign key (including satisfying the constraint). Moberly's suggestion of creating a dummy record with primary key value of -1 would work, for example. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---