django rest framework xml特殊字符

时间:2014-08-30 02:20:29

标签: python xml django django-rest-framework

我有一个CharField模型,它有时会有一个特殊字符(遗留应用程序)。

class Jobmst(models.Model):
    jobmst_id = models.IntegerField(primary_key=True, db_column='jobmst_id')
    jobmst_type = models.SmallIntegerField()
    jobmst_prntid = models.IntegerField(blank=True, null=True)
    jobmst_active = models.CharField(max_length=1, blank=True)
    evntmst_id = models.ForeignKey(Evntmst, db_column='evntmst_id', related_name='calendar_definition', verbose_name='calendar', blank=True, null=True)
    jobmst_evntoffset = models.SmallIntegerField(blank=True, null=True)
    jobmst_name = models.CharField(blank=True)
    jobmst_mode = models.SmallIntegerField(blank=True, null=True)
    jobmst_owner = models.ForeignKey(Owner, db_column='jobmst_owner', verbose_name='owner')
    jobmst_desc = models.CharField(blank=True) # This field type is a guess.
    jobmst_crttm = models.DateTimeField()
    jobdtl_id = models.OneToOneField(Jobdtl, db_column='jobdtl_id', verbose_name='details')
    jobmst_lstchgtm = models.DateTimeField(blank=True, null=True)
    jobmst_runbook = models.CharField(blank=True) # This field type is a guess.
    jobcls_id = models.IntegerField(blank=True, null=True)
    jobmst_prntname = models.CharField(blank=True)
    jobmst_alias = models.CharField(max_length=10, blank=True)
    jobmst_dirty = models.CharField(max_length=1, blank=True)
    def __unicode__(self):
        return self.jobmst_name   
    class Meta:
        managed = False
        db_table = 'jobmst'

如果我尝试使用Django Rest Framework GET的标准XML格式,则说它没有设置不正确的XML。

XML Parsing Error: not well-formed
Location: http://dviappvmtca01:9001/deploy/?format=xml&jobname=\.File%20Download%20Templates\Vendor_Download\FTP_NO_Compress_NO_Encrypt
Line Number 2, Column 5119:<root><list-item><jobmst_id>83257</jobmst_id><jobmst_type>1</jobmst_type><jobmst_prntid>75435</jobmst_prntid><jobmst_active>N</jobmst_active><evntmst_id><evntmst_id>1</evntmst_id><evntmst_name>Daily                                                       </evntmst_name><evntmst_desc></evntmst_desc><evntmst_owner>2</evntmst_owner><evntmst_lstchgtm>2009-12-04 12:28:52</evntmst_lstchgtm><evntmst_lstcmptm>2014-08-28 12:00:29</evntmst_lstcmptm><evntmst_fromdt></evntmst_fromdt><evntmst_untildt>2012-12-31 00:00:00</evntmst_untildt><evntmst_frcstdt>2012-12-31 00:00:00</evntmst_frcstdt><evntmst_type>2</evntmst_type><evntmst_subtype></evntmst_subtype><evntmst_freq>1</evntmst_freq><evntmst_crttm>2009-12-04 11:22:03</evntmst_crttm><evntmst_totcnt></evntmst_totcnt><evntmst_public>Y</evntmst_public><evntmst_months>YYYYYYYYYYYY</evntmst_months><evntmst_weeks>NNNNN</evntmst_weeks><evntmst_monthdays>YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY</evntmst_monthdays><evntmst_weekdays>YYYYYYY</evntmst_weekdays><evntmst_offset>0</evntmst_offset><evntmst_intsect>N</evntmst_intsect><evntmst_occur>0</evntmst_occur><evntmst_timeframe>0</evntmst_timeframe><evntmst_calendar>0</evntmst_calendar><evntmst_fiscal>0</evntmst_fiscal></evntmst_id><jobmst_evntoffset></jobmst_evntoffset><jobmst_name>FTP_NO_Compress_NO_Encrypt</jobmst_name><jobmst_mode>0</jobmst_mode><jobmst_owner><owner_id>913</owner_id><owner_type>2</owner_type><owner_name>DMO                           </owner_name><owner_allagents>N</owner_allagents></jobmst_owner><jobmst_desc></jobmst_desc><jobmst_crttm>2012-05-04 16:35:00</jobmst_crttm><jobdtl_id><jobdtl_id>83257</jobdtl_id><jobdtl_cmd></jobdtl_cmd><jobdtl_envfile></jobdtl_envfile><jobdtl_retnsn>180</jobdtl_retnsn><jobdtl_allowadhoc>Y</jobdtl_allowadhoc><jobdtl_waitop>N</jobdtl_waitop><jobdtl_fromdt>1899-12-30 00:00:00</jobdtl_fromdt><jobdtl_untildt>1899-12-30 00:00:00</jobdtl_untildt><jobdtl_fromtm>1899-12-30 00:15:00</jobdtl_fromtm><jobdtl_untiltm>1899-12-30 23:45:00</jobdtl_untiltm><jobdtl_proxy><usrmst_id>864</usrmst_id><usrmst_domain>CPPIB               </usrmst_domain><usrmst_name>svc_tidal_dev</usrmst_name><usrmst_fullname>svc_tidal_dev</usrmst_fullname><usrmst_desc></usrmst_desc><usrmst_phoneno></usrmst_phoneno><usrmst_pagerno></usrmst_pagerno><usrmst_email></usrmst_email><usrmst_emailtype>0</usrmst_emailtype><secmst_id>1</secmst_id><lngmst_id>1</lngmst_id><usrmst_password>@@gy-pYNnr&lt;nL'SSWAYY</usrmst_password><usrmst_externid></usrmst_externid><usrmst_suser>Y</usrmst_suser><usrmst_lstchgtm>2014-05-21 09:58:42</usrmst_lstchgtm><usrmst_sappassword></usrmst_sappassword><usrmst_pspassword></usrmst_pspassword><usrmst_aspassword></usrmst_aspassword><usrmst_orapassword></usrmst_orapassword><usrmst_wingroup>N</usrmst_wingroup></jobdtl_proxy><jobdtl_proxy2></jobdtl_proxy2><jobdtl_interval></jobdtl_interval><jobdtl_intervalcnt></jobdtl_intervalcnt><jobdtl_unit></jobdtl_unit><jobdtl_duration>729</jobdtl_duration><jobdtl_concur>1</jobdtl_concur><jobdtl_priority>50</jobdtl_priority><jobdtl_minrun>60</jobdtl_minrun><jobdtl_maxrun>60</jobdtl_maxrun><jobdtl_failalarm></jobdtl_failalarm><nodmst_id></nodmst_id><nodlstmst_id><nodlstmst_id>17</nodlstmst_id><nodlstmst_name>DMO_FTPServers                </nodlstmst_name><nodlstmst_desc></nodlstmst_desc><nodlstmst_type>3</nodlstmst_type><nodlstmst_prntid></nodlstmst_prntid><nodlstmst_seq></nodlstmst_seq><nodlstmst_ostype>1</nodlstmst_ostype><nodlstmst_lastused></nodlstmst_lastused><nodlstmst_lstchgtm>2014-04-23 21:49:29</nodlstmst_lstchgtm><servicemst_id></servicemst_id></nodlstmst_id><jobdtl_inhevent>N</jobdtl_inhevent><jobdtl_inhoptions>Y</jobdtl_inhoptions><jobdtl_inhagent>N</jobdtl_inhagent><jobdtl_inhrepeat>Y</jobdtl_inhrepeat><jobdtl_inhtime>N</jobdtl_inhtime><jobdtl_timewin>4</jobdtl_timewin><jobdtl_saveoutput>Y</jobdtl_saveoutput><jobdtl_outputname></jobdtl_outputname><jobdtl_trackmethod>1</jobdtl_trackmethod><jobdtl_trackcmd></jobdtl_trackcmd><jobdtl_deplogic>1</jobdtl_deplogic><jobdtl_rerun></jobdtl_rerun><jobdtl_params>Vendor=Vendor,FTPAddress=ftp.vendor.com,FTPUser=userid,LocalFilename=filename.&lt;JobDate.yyyymmdd&gt;.txt,RemoteFilename=filename.&lt;JobDate.yyyymmdd&gt;.txt,LocalPath=&lt;DMO_CR.1750&gt;\&lt;Vendor_SUBSET.xxxx&gt;\&lt;JobDate.yyyy\mm\dd&gt;,RemotePath=/</jobdtl_params><jobdtl_sapcount></jobdtl_sapcount><jobdtl_normalexit>0</jobdtl_normalexit><jobdtl_normalrange>0</jobdtl_normalrange><jobdtl_normalop>1</jobdtl_normalop><jobdtl_deprerun>N</jobdtl_deprerun><jobdtl_carryover>0</jobdtl_carryover><jobdtl_psjob></jobdtl_psjob><jobdtl_savelogonly>N</jobdtl_savelogonly><jobdtl_trxid></jobdtl_trxid><jobdtl_rerunok>Y</jobdtl_rerunok><jobdtl_workdir></jobdtl_workdir><jobdtl_extinfo></jobdtl_extinfo><servicemst_id></servicemst_id><jobdtl_estmethod>1</jobdtl_estmethod><jobdtl_nearoutage>3</jobdtl_nearoutage><jobdtl_trackcl></jobdtl_trackcl><jobdtl_statuscl></jobdtl_statuscl><jobdtl_abrtonclderr></jobdtl_abrtonclderr><jobdtl_estdurexclude>0</jobdtl_estdurexclude></jobdtl_id><jobmst_lstchgtm>2014-08-23 21:31:32</jobmst_lstchgtm><jobmst_runbook>https://wiki:8443/display/IO/MDS+Tidal-FTP+Troubleshooting</jobmst_runbook><jobcls_id>65</jobcls_id><jobmst_prntname>\.File Download Templates\Vendor_Download</jobmst_prntname><jobmst_alias>83257     </jobmst_alias><jobmst_dirty> </jobmst_dirty><job_dependencies></job_dependencies><job_events></job_events></list-item></root>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

原因是如果你在数据库上对jobmst_runbook字段执行SQL查询,如果它包含一个URL,则有一个隐藏字符 -

https://wiki:8443/display/IO/MDS+Tidal-FTP+Troubleshooting

如何让模型自动删除特殊字符,还是从视图中删除?或者有没有办法更改XML渲染器的编码?

1 个答案:

答案 0 :(得分:0)

我最后使用序列化程序剥离了字符代码 -

class DefinitionSerializer(serializers.ModelSerializer):
    runbook_url = serializers.SerializerMethodField('get_url')
#    dependencies = serializers.RelatedField(many=True)
    jobdep = serializers.HyperlinkedRelatedField(
        source='jobdep_set', # this is the model class name (and add set, this is how you call the reverse relation of bar)
        view_name='jobdep-detail' # the name of the URL, required
    )
#    triggers = serializers.RelatedField(many=True)
    trgmst = serializers.HyperlinkedRelatedField(
        source='trgmst_set', # this is the model class name (and add set, this is how you call the reverse relation of bar)
        view_name='trgmst-detail' # the name of the URL, required
    )

    class Meta:
        model = Jobmst
        resource_name = 'jobmst'
        depth = 2
        fields = ('jobmst_id', 'jobmst_type', 'jobmst_prntid', 'jobmst_active', 'evntmst_id',
                    'jobmst_evntoffset', 'jobmst_name', 'jobmst_mode', 'jobmst_owner', 'jobmst_desc',
                    'jobmst_crttm', 'jobdtl_id', 'jobmst_lstchgtm', 'runbook_url', 'jobcls_id', 'jobmst_prntname',
                    'jobmst_alias', 'jobmst_dirty', 'job_dependencies', 'job_events')

    def get_url(self, obj):
        if obj.jobmst_runbook == None:
            pass
        else:
            return force_text(obj.jobmst_runbook[:-5])