I m using Django (specifically django-admin) to be the admin panel for a site that uses PHP for client-facing features. I ve been messing around making the admin look exactly the way I want and so far so good. However, I ve run into a few problems that I need solving.
Here s the models that I m working with:
class Permissions(models.Model):
id = models.IntegerField(primary_key=True)
league_key = models.CharField(max_length=15)
commissioner_id = models.ForeignKey(Accounts, db_column= commissioner_id ,
to_field= id )
status = models.IntegerField()
key = models.CharField(max_length=50)
publisher_key = models.CharField(max_length=25)
publisher_display_name = models.CharField(max_length=50)
base_league = models.ForeignKey( self , db_column= id )
share = models.IntegerField()
default_fixture_key = models.CharField(max_length=50)
def __unicode__(self):
return self.publisher_key + / + self.league_key
class Meta:
db_table = u permissions
verbose_name = Permissions
verbose_name_plural = Permissions
class PermissionsAdmin(admin.ModelAdmin):
list_display = ( league_key , publisher_key , commissioner_id , status ,
base_league , share , default_fixture_key )
list_display_links = ( league_key , commissioner_id , base_league )
exclude = ( id ,)
First problem is that the admin form for editing an existing record is marking one of the fields as required. How do I tell the django-admin when a field is required and not required?
Second problem I am running into is that when I tell it to Save this record, I get the following error: duplicate key value violates unique constraint "permissions_pkey". That leads me to think that Django is not doing an update, it s trying to do an INSERT
It also occurred to me that this might be a problem related to Postgresql. permissions_pkey is a constraint on that table, keeping track of the sequence being used for auto-incrementing the id for that table
While the Django docs are awesome, they don t seem to have the info I need to figure this out.
(EDIT: Digging into the stack trace, I found this awesome little nugget:
sql
UPDATE "permissions" SET "league_key" = E l.1258472565 , "commissioner_id" = 7,
"status" = 0, "key" = E cfcd208495d565ef66e7dff9f98764da ,
"publisher_key" = E chrishartjes.com ,
"publisher_display_name" = E Chris Hartjes Free Press ,
"id" = 744, "share" = 0,
"default_fixture_key" = E WHERE "permissions"."id" = 745
which leads me to think that my little ForeignKey to itself entry is causing the problem)