Opened 7 years ago

Closed 7 years ago

#11846 closed (invalid)

field check does not wok

Reported by: ankit Owned by: nobody
Component: Forms Version: 1.1-beta
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Karen Tracey)

I am having a class which is defined as

class ABC(models.Model):
        id = models.CharField(max_length=100, primary_key=True)
        pub = models.ForeignKey(WapUser, null=True)
        brief_description = models.TextField(null=True,blank=True)
        status = models.CharField(max_length=12, null=True, help_text='Select the appropriate status and click on save', choices=SITE_STATUS_CHOICES )
        created_on = models.DateTimeField(null=True)
        pub_share = models.DecimalField('site_rev_share',max_digits=4,decimal_places=2,null=True)
        require_full_size_banner = models.IntegerField("Full Size Banner Ads", choices=BOOLEAN_CHOICES, default = 0,null=True)
        modified_on =  models.DateTimeField(auto_now=True,auto_now_add=True,verbose_name ='Last_modified_on')
        critical_nfr = models.DecimalField(max_digits=5,decimal_places=3,default='60',help_text='Plesae Enter in the range of (0-100)')
        class Meta:
                db_table = u'wap_abc'
                verbose_name_plural= "ABC"
                managed = False
        def __unicode__(self):
                return self.status
        def get_absolute_url(self):
                return "/abc/%i/" %

And looks like

class SiteForm(ModelForm):
        class Meta:
                model = ABC
        reason_for_reject = CharField(widget=forms.Textarea,max_length=500,help_text='Please enter if the reason is not in the list',required=False)
     def clean(self):
                cleaned_data = self.cleaned_data
                newStatus = self.cleaned_data.get('status')
                id_a = self.cleaned_data.get('id')
                critical_nfr = self.cleaned_data.get('critical_nfr')
                if new_pub_share == 0 or new_pub_share >=100:
                        raise forms.ValidationError('You cannot set pub_share either equal to 0% or greater than equal to 100% !')
                if critical_nfr <0  and critical_nfr is not None:
                        raise forms.ValidationError('You cannot set critical_nfr less than Zero !')
                if self.instance and self.instance.status=='pending':
                        if newStatus =='activated' or newStatus == 'rejected':
                                if newStatus == 'rejected':
                                        if rejection_code is None and new_rejection_reason=='':
                                                raise forms.ValidationError('Please select a reason to reject or else write in the box given below  !')
                                        if rejection_code != None and new_rejection_reason!='':
                                                raise forms.ValidationError('You cannot select both, either select a reason or write it !')

now if i enter the value of critical_nfr<0,it gives error but if i give '-0' then it does not give any error.WHy .-0 is not any number

Change History (5)

comment:1 Changed 7 years ago by Karen Tracey

Description: modified (diff)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: newclosed

Fixed formatting. PLEASE use preview and follow the Wiki Formatting link to learn how to format things properly.

-0 is a valid decimal as far as Python is concerned:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal("-0")
>>> x = Decimal("-0")
>>> y = Decimal("1")
>>> x*y
>>> x+y
>>> y-x
>>> y+x

It's going to behave just like 0 so there shouldn't be any problem with accepting it. Django is not going add additional requirement for "valid decimal" beyond what Python already imposes.

If you want to get rid of the negative sign for display purposes you can always just multiply zero-valued critical_nfr values with themselves to ensure they have positive sign. (If you are determined to reject such entries you can use the is_signed Decimal method to detect the situation:

comment:2 Changed 7 years ago by ankit

But in the DB table it is being stored as -0.I know that this will not have any problem but logically there is no number as -0.Well to overcome this problem i am using abs value while saving

comment:3 Changed 7 years ago by ankit

I am using postgres and my db column is double precision

comment:4 Changed 7 years ago by anonymous

Resolution: invalid
Status: closedreopened

comment:5 Changed 7 years ago by Karen Tracey

Resolution: invalid
Status: reopenedclosed
Note: See TracTickets for help on using tickets.
Back to Top