I have this model for my accounting app:
class Simpleunits(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
symbol = models.CharField(max_length=32)
formal = models.CharField(max_length=32)
class Compoundunits(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
firstunit = models.ForeignKey(Simpleunits,on_delete=models.CASCADE)
conversion = models.DecimalField(max_digits=19,decimal_places=2)
secondunit = models.ForeignKey(Simpleunits,on_delete=models.CASCADE)
class Stockdata(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
stock_name = models.CharField(max_length=32)
unitsimple = models.ForeignKey(Simpleunits,on_delete=models.CASCADE,null=True,blank=True)
unitcomplex = models.ForeignKey(Compoundunits,on_delete=models.CASCADE,null=True,blank=True)
I want to create a custom validation method under model class Stockdata that if the user mention both unitsimple and unitcomplex then they will get a validation error that "Only one unit should be given" and vice versa...
I mean to say that the user can only mention one unit either unitsimple or unitcomplex if they mention both then they will get a validation error...
Do anybody have any idea what should I do under def clean(self) function to accomplish this..???
Thank you in advance...