Code

Opened 5 years ago

Closed 5 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 kmtracey)

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/" % self.id

And form.py 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

Attachments (0)

Change History (5)

comment:1 Changed 5 years ago by kmtracey

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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")
Decimal("-0")
>>> x = Decimal("-0")
>>> y = Decimal("1")
>>> x*y
Decimal("-0")
>>> x+y
Decimal("1")
>>> y-x
Decimal("1")
>>> y+x
Decimal("1")
>>> 

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: http://docs.python.org/library/decimal.html#decimal.Decimal.is_signed.)

comment:2 Changed 5 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 5 years ago by ankit

I am using postgres and my db column is double precision

comment:4 Changed 5 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:5 Changed 5 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from reopened to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.