2012-06-01 78 views
0

我想提出一個Ajax請求到意見如下:強迫爲Unicode:需要字符串或緩衝區,NoneType發現

def all_json_models(request): 
    data = {} 
    try: 

    isp = request.GET['status'] 
    present_isp = Priority.objects.filter(ispname = isp) 
    isp_count = MultiWAN.objects.all() 

    # data['latest_no_rules'] = latest_no_rules 
    #data['present_isp'] = present_isp 
    data['isp_count'] = isp_count 

    return HttpResponse(simplejson.dumps(data)) 

models.py就像

class MultiWAN(models.Model): 

    isp_name = models.CharField(max_length=10) 
    description = models.TextField(null=True) 
    ip_address = models.IPAddressField(null=True) 
    subnet  = models.IPAddressField(null=True) 
    gateway  = models.IPAddressField(null=True) 
    nameserver = models.ForeignKey('NameServer') 
    weight  = models.IntegerField(null=False) 
    interface = models.CharField(max_length=5) 
    def __unicode__(self): 
     """ 
     This function is to return the values we required. 
     Arguments: 
     - `self`: 
     """ 
     # return u'%s ' % (self.isp_name) 


class NameServer(models.Model): 
    """ A Isp can have more than one nameserver so far we are declearing a seperate table 
    """   
    name = models.IPAddressField(null=False)  




class Priority(models.Model): 
    priority = models.IntegerField(null = True) 
    ispname = models.ForeignKey('MultiWAN') 
    rule = models.CharField(max_length=5,null=False) 
    From = models.IPAddressField(null=True) 
    To = models.IPAddressField(null=True) 
    def __unicode__(self): 
         return u'%s ' % (self.priority) 

同時使請求我收到錯誤:

"coercing to Unicode: need string or buffer, NoneType found" 

我在做什麼錯在這裏?

+0

請顯示a)有效的Python代碼,正確縮進;和b)你得到的追蹤。 –

回答

0

沒有完整的回溯很難說(因爲它提供了有關在您的代碼中拋出異常的信息)。

錯誤消息「強制爲Unicode:需要字符串或緩衝區,找不到類型」意味着,在某些時候,django試圖將某些內容轉換爲unicode,並期望得到一個字符串,但收到None。這意味着要麼您調用傳遞None而不是字符串的函數,或者您的某個方法返回None而不是字符串。

在您向我們展示的代碼中,MultiWAN.__unicode__似乎不明確。也許錯誤源於此?

相關問題