0

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...

1 Answer 1

1

Create a model form for the create view of Stockdata and as you said, add a custom clean() method as given below.

class CreateStockData(forms.ModelForm):
   class Meta:
       model = Stockdata        
       fields= [....]
   ....
   def clean(self):
       cleaned_data = super(CreateStockData, self).clean()
       unitsimple = cleaned_data.get('unitsimple')
       unitcomplex = cleaned_data.get('unitcomplex')
       if unitsimple != None and unitcomplex != None:
           raise forms.ValidationError({'unitcomplex':["You are not supposed to select both values!"]})

Edit

In view of your comment, let me post the other way.

class Stockdata(models.Model):
    ....
    def clean(self):
        if self.unitsimple is not None and if self.unitcomplex is not None:
            raise ValidationError(
                {'unitcomplex':["You are not supposed to select both values!"]})
    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

See Validating objects

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah that can be done...But it is also possible in doing in model...I have done that...I only want to know the logic which you have provided...Thank you@art06
How do you display the error message to the form when you do it inside model?
I am connecting the model to the form under class meta and in template I have done this {{ form.non_field_errors }}...And that worked...The error message is showing in template and the user cannot submit the form...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.