IntegrityError at /player/edit/2
UNIQUE constraint failed: players_player.id
I’ve set up a CRUD interface using generic views (CreateView, DetailView…) and am having trouble with the UpdateView (which I call EditView).
IntegrityError at /player/edit/2/
UNIQUE constraint failed: players_player.id
I have a default number for the player ranking, though I want that to be automatically determined based on the number of available players. When I try to update it on the form, I get the above error. I’m puzzled by this error since I’m not trying to create a new player. Also, if I open the SQLite database using a DB reader, it looks like I simply have four records with PKs 1 - 4. Why does Django think I’m violating the uniqueness constraint?
Here is the model:
class Player(models.Model):
# Availability choice list
available = ‘AVL’
injured = ‘INJ’
away = ‘AWY’
retired = ‘RET’
AVAILABILITY_CHOICES = [
(available, ‘available’),
(injured, ‘injured’),
(away, ‘out of town’),
(retired, ‘retired’)
]
# Player fields
first = models.CharField('First Name', max_length=30)
last = models.CharField('Last Name', max_length=30)
cell = models.CharField('Cell Phone', max_length=12)
email = models.EmailField('Email') # changed field type
availability = models.CharField('Availability', choices = AVAILABILITY_CHOICES, \
max_length = 15, default='AVL')
ranking = models.IntegerField(default = 99)
def __str__(self):
full_name = self.first + ' ' + self.last + ' ' + str(self.ranking)
return full_name
the urlpatterns list contains:
path(‘player/edit/int:pk/’, PlayerEditView.as_view(), name=‘player_edit’),
class PlayerListView(ListView):
model = Player
template_name = ‘players_list.html’
…
class PlayerEditView(UpdateView):
model =Player
context_object_name = ‘player’
fields = [‘ranking’, ‘first’, ‘last’, ‘cell’, ‘email’, ‘availability’]
template_name = ‘player_edit.html’
…
And the template contains this relevant portion…
Cancel |