Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with updating records in django-admin
    text
    copied!<p>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.</p> <p>Here's the models that I'm working with:</p> <pre><code>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',) </code></pre> <p>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?</p> <p>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</p> <p>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</p> <p>While the Django docs are awesome, they don't seem to have the info I need to figure this out.</p> <p>(EDIT: Digging into the stack trace, I found this awesome little nugget:</p> <pre><code>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 ' </code></pre> <p>which leads me to think that my little ForeignKey to itself entry is causing the problem)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload