如何在Django中删除数据库结果中的斜杠..?

时间:2014-12-18 08:47:18

标签: python django

我有一个类似的模型类:

class SpUserDetails(models.Model):
    class Meta:
        db_table = 'service_provider_details' 
        app_label = 'api'
    user = models.ForeignKey(SpUsers, related_name='details') 
    designation = models.CharField(max_length=100,null=True, blank=True)
    company_name = models.CharField(max_length=50,null=True, blank=True)
    description = models.CharField(max_length=2000,null=True, blank=True)
    dob = models.DateField(null=True, blank=True)
    years_of_experience = models.IntegerField(null=True, blank=True)
    prof_membership = models.CharField(max_length=255,null=True, blank=True)
    professional_license_number = models.CharField(max_length=50,null=True, blank=True)
    degrees = models.CharField(max_length=255,null=True, blank=True)
    awards_and_publication = models.CharField(max_length=255,null=True, blank=True)
    auth_to_issue_insurence_rem_receipt = models.SmallIntegerField(null=True, blank=True)
    auth_to_bill_insurence_copany = models.SmallIntegerField(null=True, blank=True)

    treatment_for_physically_disabled_person = models.SmallIntegerField(null=True, blank=True)
    specialties = models.CharField(max_length=255,null=True, blank=True)
    offering_at_work_office = models.SmallIntegerField(null=True, blank=True)
    offering_at_home = models.SmallIntegerField(null=True, blank=True)

    def __unicode__(self):

        return '{"id":%d,"user_id":%s,"designation":"%s","company_name":"%s","description":"%s","dob":"%s","years_of_experience":"%s","prof_membership":"%s","awards_and_publication":"%s","auth_to_issue_insurence_rem_receipt":"%s","auth_to_bill_insurence_copany":"%s","degrees":"%s","treatment_for_physically_disabled_person":"%s","professional_license_number":"%s","specialties":"%s","offering_at_home":"%s","offering_at_work_office":"%s"}' % (self.id,self.user.id,self.designation, self.company_name,self.description,self.dob,self.years_of_experience,self.prof_membership,self.awards_and_publication,self.auth_to_issue_insurence_rem_receipt,self.auth_to_bill_insurence_copany,self.degrees,self.treatment_for_physically_disabled_person,self.professional_license_number,self.specialties,self. offering_at_home,self.offering_at_work_office)

现在我想从self.description删除斜线,我尝试过.decode(' string_escape'),如:

def __unicode__(self):
        self.description = self.description.decode('string_escape')
        return '{"id":%d,"user_id":%s,"designation":"%s","company_name":"%s","description":"%s","dob":"%s","years_of_experience":"%s","prof_membership":"%s","awards_and_publication":"%s","auth_to_issue_insurence_rem_receipt":"%s","auth_to_bill_insurence_copany":"%s","degrees":"%s","treatment_for_physically_disabled_person":"%s","professional_license_number":"%s","specialties":"%s","offering_at_home":"%s","offering_at_work_office":"%s"}' % (self.id,self.user.id,self.designation, self.company_name,self.description,self.dob,self.years_of_experience,self.prof_membership,self.awards_and_publication,self.auth_to_issue_insurence_rem_receipt,self.auth_to_bill_insurence_copany,self.degrees,self.treatment_for_physically_disabled_person,self.professional_license_number,self.specialties,self. offering_at_home,self.offering_at_work_office)

但它给出了以下错误:

'ascii' codec can't encode character u'\xa0' in position 24: ordinal not in range(128)

请帮助.. !!

1 个答案:

答案 0 :(得分:0)

经过大量搜索,我找到了解决方案:

def __unicode__(self):
        if self.description is not None:
            self.description = self.description.replace("\\", "")
        return '{"id":%d,"user_id":%s,"designation":"%s","company_name":"%s","description":"%s","dob":"%s","years_of_experience":"%s","prof_membership":"%s","awards_and_publication":"%s","auth_to_issue_insurence_rem_receipt":"%s","auth_to_bill_insurence_copany":"%s","degrees":"%s","treatment_for_physically_disabled_person":"%s","professional_license_number":"%s","specialties":"%s","offering_at_home":"%s","offering_at_work_office":"%s"}' % (self.id,self.user.id,self.designation, self.company_name,self.description,self.dob,self.years_of_experience,self.prof_membership,self.awards_and_publication,self.auth_to_issue_insurence_rem_receipt,self.auth_to_bill_insurence_copany,self.degrees,self.treatment_for_physically_disabled_person,self.professional_license_number,self.specialties,self. offering_at_home,self.offering_at_work_office)

它对我来说很完美.. !!