Django将外键保存在XML模型中

时间:2014-01-07 11:32:38

标签: xml django foreign-keys

我是Django的新手,所以如果我问一些简单的话,我很抱歉。我有一个模特"药物"和模型"填充"。填充与药物与foreign_key" medication_id"相关联。我试图用以下函数保存一个填充但是当我在数据库中搜索时,drug_id是空白的。你能帮助我吗?谢谢!

def add_fill(request,medication_id):  
if request.method == "GET":  
    return render_template('newfill')  
else:  

     # Fix dates formatted by JQuery into xs:dateTime  

    date = request.POST['date'] + 'T00:00:00Z' if request.POST['date'] != '' else ''  

    #endDate = request.POST['endDate'] + 'T00:00:00Z' if request.POST['endDate'] != ''   else ''  

    # get the variables and create a problem XML  

    params = {'date': request.POST['date'],
              'dispenseDaysSupply': request.POST['dispenseDaysSupply'],
              'pbm': request.POST['pbm'],
              'medication_id': medication_id,
              'pharmacy_ncpdpid': request.POST['pharmacy_ncpdpid'],
              'pharmacy_org': request.POST['pharmacy_org'],
              'pharmacy_adr_country': request.POST['pharmacy_adr_country'],
              'pharmacy_adr_city': request.POST['pharmacy_adr_city'],
              'pharmacy_adr_postalcode': request.POST['pharmacy_adr_postalcode'],
              'pharmacy_adr_street': request.POST['pharmacy_adr_street'],
              'provider_dea_number': request.POST['provider_dea_number'],
              'provider_npi_number': request.POST['provider_npi_number'],
              'provider_email': request.POST['provider_email'],
              'provider_name_given': request.POST['provider_name_given'],
              'provider_name_family': request.POST['provider_name_family'],
              'provider_tel_1_type': request.POST['provider_tel_1_type'],
              'provider_tel_1_number': request.POST['provider_tel_1_number'],
              'provider_tel_1_preferred_p': request.POST['provider_tel_1_preferred_p'],
              'quantityDispensed_value': request.POST['quantityDispensed_value'],
              'quantityDispensed_unit': request.POST['quantityDispensed_unit']}
    medication_xml = render_raw('fill', params, type='xml')

    # add the problem
    client = get_indivo_client(request)
    resp, content = client.document_create(record_id=request.session['record_id'],body=medication_xml,
                                           content_type='application/xml')


    if resp['status'] != '200':
        # TODO: handle errors
        raise Exception("Error creating new fill: %s"%content)

    # add a notification

    client.record_notify(record_id=request.session['record_id'],
                          body={'content':'a new fill has been added to your problem list'})

    return HttpResponseRedirect(reverse(medication_list))



model fill:

<Models xmlns="http://indivo.org/vocab/xml/documents#">
 <Model name="Fill">
  <Field name="date">{{date}}</Field> 
  <Field name="dispenseDaysSupply">{{dispenseDaysSupply}}</Field>
  <Field name="pbm">{{pbm}}</Field>
  <Field name="pharmacy_ncpdpid">{{pharmacy_ncpdpid}}</Field>
  <Field name="pharmacy_org">{{pharmacy_org}}</Field>
  <Field name="pharmacy_adr_country">{{pharmacy_adr_country}}</Field>
  <Field name="pharmacy_adr_city">{{pharmacy_adr_city}}</Field>
  <Field name="pharmacy_adr_postalcode">{{pharmacy_adr_postalcode}}</Field>
  <Field name="pharmacy_adr_street">{{pharmacy_adr_street}}</Field>
  <Field name="provider_dea_number">{{provider_dea_number}}</Field>
         <Field name="provider_npi_number">{{provider_npi_number}}</Field>
          <Field name="provider_email">{{provider_email}}</Field>
          <Field name="provider_name_given">{{provider_name_given}}</Field>
          <Field name="provider_name_family">{{provider_name_family}}</Field>
          <Field name="provider_tel_1_type">{{provider_tel_1_type}}</Field>
          <Field name="provider_tel_1_number">{{provider_tel_1_number}}</Field>
          <Field name="provider_tel_1_preferred_p">{{provider_tel_1_preferred_p}}      </Field>
          <Field name="quantityDispensed_value">{{quantityDispensed_value}}</Field>
          <Field name="quantityDispensed_unit">{{quantityDispensed_unit}}</Field>
  </Model>
</Models>

`

render_raw:  

def render_raw(template_name, vars, type):
  """
  rendering a template into a string
  """
  t_obj = loader.get_template('%s/%s.%s' % (settings.TEMPLATE_PREFIX,template_name, type))
  c_obj = Context(vars)
  return t_obj.render(c_obj)

0 个答案:

没有答案