Home > OS >  Django Model Data dynamic with column name and value
Django Model Data dynamic with column name and value

Time:09-22

During django create new records in dynamic way got error, ID is expection number but got 'OrderNo= "Order No", OrderSuffix= "Order Suffix", Reference= "Reference", ItemCode= "Item Code", QtyOrdered= "Qty Ordered", QtyShipped= "Qty Shipped", Warehouse= "Warehouse", DispatchDate= "Dispatch Date", TrackingNo= "Tracking No", CarrierCode= "Carrier Code"'

But if i put this string like below its working fine

data = shipment(OrderNo= "Order No", OrderSuffix= "Order Suffix", Reference= "Reference", ItemCode= "Item Code", QtyOrdered= "Qty Ordered", QtyShipped= "Qty Shipped", Warehouse= "Warehouse", DispatchDate= "Dispatch Date", TrackingNo= "Tracking No", CarrierCode= "Carrier Code") #shipment is the model
for row in table:
            ind = 0
            rowdata = ''
            for col in config['header_columns']:
                rowdata  = col['title']   '= "'   row[ind]   '"'   ', '
                ind = ind   1
            rowdata = rowdata[:-2]
            #return HttpResponse(rowdata)
            data = shipment(rowdata) #shipment is the model
            data.save(using="integration")

enter image description here

That means created string variable if i put directly for make it dynamic its showing "ID" expected a number but the string if i put its totally working fine.

May anyone help me.Thanks in Advance.

CodePudding user response:

Producing a string that looks like Python code doesn't work. You want to produce a dict which you then unpack into separate arguments, e.g.:

for row in table:
    rowdata = dict((col['title'], i) for col, i in zip(config['header_columns'], row))
    data = shipment(**rowdata)
  • Related