django rest framework xml special character

2019-08-01 21:35发布

问题:

I have a model with a CharField that at times has a special character (legacy application).

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'

If I try to use the standard XML format for Django Rest Framework GET it fails saying it's improperly designed 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>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

The reason for this is that if you do an SQL Query on the DB the jobmst_runbook field, if it contains a URL, has a hidden character -

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

How do I get the model to automatically remove the special character, or do I do it from the view? Alternatively is there a way to change the encoding for the XML renderer?

回答1:

I ended up stripping the character code using my serializer -

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])