class Pricing(models.Model):
price = models.DecimalField(max_digits=4, decimal_places=1)
is_active = models.BooleanField(default=False)
def __str__(self):
return str(self.price)
class Services(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=30)
pricing = models.OneToOneField(Pricing, on_delete=models.CASCADE, default=0.0)
categories = models.ManyToManyField(Category, related_name="services")
is_active = models.BooleanField(default=False)
def __str__(self):
return self.name
In my case you are getting this error in the function where you are testing aan instance onetoonefield : where you're creating ana price instance and adding it into 2 service instances and getting this error:
Solution : Inside your function do this
with transaction.atomic():
with self.assertRaises(IntegrityError):
service = Services.objects.create(name = "Some Service", pricing= self.pricing1)