2017-09-04 48 views
0

我得到一個不是日期格式字段的表單字段的格式錯誤。我不知道爲什麼它給我的不相關的表單字段我試圖填補一些表格...格式+關鍵錯誤 - 餘額 - Django

這裏是顯示了確切的錯誤:

ValidationError at /transfer/ 
["'0' value has an invalid date format. It must be in YYYY-MM-DD format."] 
Request Method: POST 
Request URL: http://localhost:8000/transfer/ 
Django Version: 1.8.6 
Exception Type: ValidationError 
Exception Value:  
["'0' value has an invalid date format. It must be in YYYY-MM-DD format."] 
Exception Location: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py in to_python, line 1287 
Python Executable: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\python.exe 
Python Version: 3.6.1 
Python Path:  
['C:\\Users\\OmarJandali\\Desktop\\opentab\\opentab', 
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib', 
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36', 
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages'] 

這裏模型文件:

class Profile(models.Model): 
    user = models.ForeignKey(User, on_delete=models.CASCADE) # server 
    first_name = models.CharField(max_length=25, default='first') 
    last_name = models.CharField(max_length=25, default='last') 
    dob = models.DateField(default='0') 
    city = models.CharField(max_length=45, default='city') # user 
    state = models.CharField(max_length=25, default='state') 
    phone = models.BigIntegerField(default=0) # user 
    privacy = models.SmallIntegerField(default=1) # user 
    balance = models.DecimalField(decimal_places=2, max_digits=9, default=0) 
    created = models.DateTimeField(auto_now_add=True) # server 

這裏是form.py:

class TransferForm(forms.ModelForm): 
    acct_choices = (('Tabz', 'Tabz - Username'), 
        ('Wells Fargo', 'Wells Fargo - Username')) 
    main = forms.TypedChoiceField(
     choices=acct_choices 
    ) 
    transfer = forms.TypedChoiceField(
     choices=acct_choices 
    ) 
    class Meta: 
     model = Transfers 
     fields = ['main', 'transfer', 'amount', 'memo'] 

這裏是處理F中的一部分ORM和創建新的簡檔:

的錯誤是在線9 - 14

if main == 'Tabz': 
        profiles = Profile.objects.all() 
        for profile in profiles: 
         if currentUser == profile.user: 
          currentProfile = profile 
          currentProfile.balance = currentProfile.balance - amount 
          currentProfile.save() 
         else: 
          new_balance = amount 
          new_profile = Profile.objects.create(
           user = currentUser, 
           balance = new_balance, 
          ) 
        message = 'You have transfered ' + amount + ' from your Tabz account to main account' 
        new_activity = Acitivty.objects.create(
         user = currentUser, 
         description = message, 
         status = 1, 
         category = 1, 
        ) 
       if transfer == 'Tabz': 
        profiles = Profile.objects.all() 
        for profile in profiles: 
         if currentUser == profile.user: 
          currentProfile = profile 
          currentProfile.balance = currentProfile.balance + amount 
          currentProfile.save() 
         else: 
          new_balance = amount 
          new_profile = Profile.objects.create(
           user = currentUser, 
           balance = new_balance 
          ) 
        message = 'You have transfered ' + amount + ' from your Tabz account to main account' 
        new_activity = Acitivty.objects.create(
         user = currentUser, 
         description = message, 
         status = 1, 
         category = 1, 
        ) 
       return redirect('home_page') 

回答

3

作爲已拍攝將DateField並且已提供0 defualt值從而錯誤即將到來,

所以這行

dob = models.DateField(default='0') 

改變

dob = models.DateField(default='1900-01-01') 

然後makemigrationsmigrate。然後嘗試提交表單

+0

「models.DateField()」的默認值應該是「datetime.date」對象,而不是字符串。 –