when I run python manage.py tests
I get this error as django.db.utils.IntegrityError: UNIQUE constraint failed: books_book.id
it causes most of the other tests to fail
What I have tried:
Here are the full tests.py file:
class BookViewTests(TestCase):
self.user=get_user_model().objects.create_user(
username='test',
email='test@gmail.com',
city='city',
state='state',
password='secret',
id_2 ='96540845',
work = 'Wipro'
)
self.book =Book.objects.create(
title='How to Win Friends and Influence People',
author='Dale Carnegie',
genre ='Self-help',
owner =self.user,
id ='75777553',
is_approved=False,
available=False
)
def test_book_detail_view(self):
self.client.login(username='test',password='secret')
resp =self.client.get(reverse('book_detail',kwargs={'book_id':75777553}))
self.assertEqual(resp.status_code,200)
self.assertTemplateUsed(resp,'book_detail.html')
My model:
class Book(models.Model):
id=models.SlugField(max_length=8,primary_key=True)
genre_choices=[(i,i) for i in ['Biography','Business','Children','Classic','Comedy','Fantasy','History','Horror','Mystery','Mythology','Poetry','Religion','Romance','SciFi','Self-Help','Spirituality','Thriller','Non-Fiction','Fiction','Others']]
title =models.CharField(max_length =255)
author =models.CharField(max_length =255,validators=[alphabet_validate])
genre =models.CharField(max_length=100,choices=genre_choices)
available =models.BooleanField(default =True)
slug = models.SlugField(verbose_name='Slug', null=True,blank=True,max_length = 200)
How to solve this issue? Thanks in Advance.