如何在TastyPie中使用null相关资源POST到服务器?

时间:2015-06-15 21:01:31

标签: django tastypie

我有以下两种型号。它们通过从TextMission表到Mission表的OneToOneField关系“任务”链接。

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:if test="((Integration/Case/SecurityGroup[not(contains(@Word,'SEAL'))]) or (not(Integration/CaseSecurityGroup))) and (/Integration/Case/CaseType[(@Word='CRM') or (@Word='VIB') or (@Word='DEL') or (@Word='D16') or (@Word='JPO') or (@Word='JTR')])">
            <xsl:apply-templates select="Integration/Case/CaseEvent[(@Op='A') and ((EventType/@Word='DLSUSPNON') or (EventType/@Word='DLSUSFTA') or (EventType/@Word='DLSUSFTP') or (EventType/@Word='DLREINSNON') or (EventType/@Word='DLREINSTAT') or (EventType/@Word='NPINDPS') or (EventType/@Word='DPSDVSCOR'))]"/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="CaseEvent">
        <!-- check for Updated Events -->
        <xsl:choose>
            <xsl:when test="(EventType/@Word='DLSUSPNON') or (EventType/@Word='DLSUSFTA')">
                <NotificationEvent notificationType="DvsDlNotice">
                    <xsl:attribute name="elementState">New</xsl:attribute>
                    <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                    <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                    <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                    <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                    <xsl:attribute name="noticeTypeCode">SFTA</xsl:attribute>
                    <xsl:text>Suspension - Fail to Appear or Pay</xsl:text>
                </NotificationEvent>
            </xsl:when>
            <xsl:when test="(EventType/@Word='DLSUSFTP')">
                <NotificationEvent notificationType="DvsDlNotice">
                    <xsl:attribute name="elementState">New</xsl:attribute>
                    <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                    <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                    <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                    <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                    <xsl:attribute name="noticeTypeCode">SFTP</xsl:attribute>
                    <xsl:text>Suspension - Fail to Pay</xsl:text>
                </NotificationEvent>
            </xsl:when>
            <xsl:when test="(EventType/@Word='DLREINSNON') or (EventType/@Word='DLREINSTAT')">
                <NotificationEvent notificationType="DvsDlNotice">
                    <xsl:attribute name="elementState">New</xsl:attribute>
                    <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                    <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                    <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                    <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                    <xsl:attribute name="noticeTypeCode">REIN</xsl:attribute>
                    <xsl:text>Reinstatement - Fail to Appear or Pay</xsl:text>
                </NotificationEvent>
            </xsl:when>
            <xsl:when test="(EventType/@Word='NPINDPS')">
                <NotificationEvent notificationType="DvsDlNotice">
                    <xsl:attribute name="elementState">New</xsl:attribute>
                    <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                    <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                    <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                    <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                    <xsl:attribute name="noticeTypeCode">SNPI</xsl:attribute>
                    <xsl:text>No Proof of Insurance Notification</xsl:text>
                </NotificationEvent>
            </xsl:when>
            <xsl:when test="(EventType/@Word='DPSDVSCOR')">
                <!--Sort the dates in descending order to get the lastest date. Convert the date string first-->
                <xsl:for-each select="/Integration/Case/CaseEvent[(Deleted='false') and ((EventType/@Word='DLSUSPNON') or (EventType/@Word='DLSUSFTA') or (EventType/@Word='DLSUSFTP') or (EventType/@Word='DLREINSNON ') or (EventType/@Word='DLREINSTAT'))]">
                    <xsl:sort select="substring(EventDate,7,4)" order="descending"/>
                    <xsl:sort select="substring(EventDate,1,2)" order="descending"/>
                    <xsl:sort select="substring(EventDate,4,2)" order="descending"/>
                    <xsl:sort select="substring-after(EventTime,' ')" order="descending"/>
                    <xsl:sort select="substring-before(EventTime,':')" order="descending" data-type="number"/>
                    <xsl:sort select="substring-before(substring-after(EventTime,':'),' ')" order="descending" data-type="number"/>
                    <xsl:sort select="substring(TimestampCreate,7,4)" order="descending"/>
                    <xsl:sort select="substring(TimestampCreate,1,2)" order="descending"/>
                    <xsl:sort select="substring(TimestampCreate,4,2)" order="descending"/>
                    <xsl:sort select="substring(TimestampCreate,12,12)" order="descending"/>
                    <xsl:if test="position() = 1">
                        <xsl:choose>
                            <xsl:when test="(EventType/@Word='DLSUSPNON') or (EventType/@Word='DLSUSFTA') or (EventType/@Word='DLSUSFTP')">
                                <xsl:if test="count(/Integration/Case/CaseEvent[(EventType/@Word='NPINDPS') and (Deleted='false')])>0">
                                    <NotificationEvent notificationType="DvsDlNotice">
                                        <xsl:attribute name="elementState">New</xsl:attribute>
                                        <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                                        <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                                        <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                                        <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                                        <xsl:attribute name="noticeTypeCode">SNPI</xsl:attribute>
                                        <xsl:text>No Proof of Insurance Notification</xsl:text>
                                    </NotificationEvent>
                                </xsl:if>
                                <xsl:choose>
                                    <xsl:when test="(EventType/@Word='DLSUSPNON') or (EventType/@Word='DLSUSFTA')">
                                        <NotificationEvent notificationType="DvsDlNotice">
                                            <xsl:attribute name="elementState">New</xsl:attribute>
                                            <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                                            <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                                            <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                                            <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                                            <xsl:attribute name="noticeTypeCode">SFTA</xsl:attribute>
                                            <xsl:text>Suspension - Fail to Appear or Pay</xsl:text>
                                        </NotificationEvent>
                                    </xsl:when>
                                    <xsl:when test="(EventType/@Word='DLSUSFTP')">
                                        <NotificationEvent notificationType="DvsDlNotice">
                                            <xsl:attribute name="elementState">New</xsl:attribute>
                                            <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                                            <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                                            <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                                            <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                                            <xsl:attribute name="noticeTypeCode">SFTP</xsl:attribute>
                                            <xsl:text>Suspension - Fail to Pay</xsl:text>
                                        </NotificationEvent>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:when test="(EventType/@Word='DLREINSNON') or (EventType/@Word='DLREINSTAT')">
                                <NotificationEvent notificationType="DvsDlNotice">
                                    <xsl:attribute name="elementState">New</xsl:attribute>
                                    <xsl:attribute name="elementName">CaseEvent</xsl:attribute>
                                    <xsl:attribute name="elementKey"><xsl:value-of select="@ID"/></xsl:attribute>
                                    <xsl:attribute name="eventDate"><xsl:value-of select="EventDate"/></xsl:attribute>
                                    <xsl:attribute name="eventTime"><xsl:value-of select="EventTime"/></xsl:attribute>
                                    <xsl:attribute name="noticeTypeCode">REIN</xsl:attribute>
                                    <xsl:text>Reinstatement - Fail to Appear or Pay</xsl:text>
                                </NotificationEvent>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:if>
                </xsl:for-each>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

有时候任务会有相应的TextMission,但并非总是如此。当我在shell中创建/更新对象时,我能够使一切正常工作,但是当有一个TextMission(PUTing为/ mission / {id} /)时,TastyPie只能正确处理请求。当text_mission = null时,它就会崩溃。

我的tastypie配置:

#Models.py
class Mission(models.Model):
    name = models.CharField(max_length = 40)

class TextMission(models.Model):
    mission = models.OneToOneField(Mission, related_name="text_mission", primary_key = True)

当将以下JSON返回给服务器时(我收到的JSON完全相同),我得到一个ValueError:

失败

  

{“name”:“TEST”,“id”:“1”,“text_mission”:null,“resource_uri”:   “/ api / web / mission / 1 /”,}   *** ValueError:无法分配无:“Mission.text_mission”不允许空值。

成功

  

{“name”:“TEST”,“id”:“1”,“text_mission”:{“id”:“1”,“resource_uri”:“/ api / web / text_mission / 1 /”}, “resource_uri”:   “/ api / web / mission / 1 /”,}

我的代码中是否存在我做错的事情,或者它不应该以这种方式工作。我在这里不知所措。

我正在运行TastyPie 0.9.11。

1 个答案:

答案 0 :(得分:1)

我最终搞清楚了,这是一个非常愚蠢的错误。除了null = True之外,我只需要添加blank = True:

class MissionResource(ModelResource):
    text_mission = fields.ToOneField('path.to.TextMissionResource', attribute='text_mission', related_name='mission', full = True, blank = True, null = True)

这使一切正常。