Sakthi Sakthi - 4 months ago 16
Python Question

Django1.5 - python2.7 -how to display and update a database having 3 foreign keys

Problem:
I have a database with 3 foreign keys, every day i have to collect the user input with the following rules;


  1. if there is already data present for today's date in db, then retrieve and display the data. the data will result in multiple rows, but each row is uniquely queried based on the 3 foreign keys (product, branch, merge_version,) and date_created (auto_add=True)

  2. if the query did not return anything, create a object with class instance and display empty rows

  3. if the user click one single submit button, the data should be updated in the database with the following rules;


    • For each row submitted, validate if there is already an existing row present in database, if there is a row, update the row, else create a new row




I tried with creating multiple form instance in a for loop, for each product, and send the dictionary to template page, and it displays the rows without any issues.

When i click submit, only the last row data is saved, since request.POST returns the following output;

QueryDict: {u'build_date': [u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'
2013-10-11'], u'cln': [u'5555', u'2222', u'2222', u'2222', u'2222', u'2222', u'2222', u'090909'], u'logs': [u'no logs', u'no logs', u'no logs', u'no logs', u'no logs', u'n
o logs', u'no logs', u'no logs'], u'rpmt_status': [u'R', u'R', u'R', u'R', u'R', u'R', u'R', u'R'], u'rpmt_result': [u'F', u'F', u'F', u'F', u'F', u'F', u'F', u'F'], u'com
ments': [u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg'], u'cbs_other_bugs': [u'666', u'666', u'666', u'666', u'666', u'666', u'666', u'666
'], u'build_num': [u'12345', u'4444', u'8888', u'8888', u'8888', u'8888', u'8888', u'080808'], u'cbs_merge_blocker': [u'76767', u'76767', u'76767', u'76767', u'76767', u'7
6767', u'76767', u'76767'], u'csrfmiddlewaretoken': [u'8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw'], u'cbs_waived': [u'89898', u'89898', u'89898', u'89898', u'89898', u'89898', u'89
898', u'89898']}


if i have individual submit button for each row i get the below request.POST

QueryDict: {u'build_date': [u'2013-10-11'], u'cln': [u'23423'], u'logs': [u'no logs'], u'rpmt_status': [u'R'], u'rpmt_result': [u'F'], u'comments': [u'hghjg'], u'cbs_other_bugs': [u'666'], u'build_num': [u'12345'], u'cbs_merge_blocker': [u'76767'], u'csrfmiddlewaretoken': [u'8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw'], u'cbs_waived': [u'89898']}


But i want to save the the entire database with one submit button.
Please tell me where i am making mistake?
or is there a best way to handle this situation other than using ModelForm?

Answer

Basically the problem is with the client side, which sends the post request. The data is not packed properly at the client side!

The javascript which creates this json data, did not honour the data exchange format expected at the server side.

The solution is to pack the data with proper dictionary format. This is how the old data format was;

{
    "build_date": [
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11"
    ],
    "build_num": [
        "12345",
        "4444",
        "8888",
        "8888",
        "8888",
        "8888",
        "8888",
        "080808"
    ],
    "cbs_merge_blocker": [
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767"
    ],
    "cbs_other_bugs": [
        "666",
        "666",
        "666",
        "666",
        "666",
        "666",
        "666",
        "666"
    ],
    "cbs_waived": [
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898"
    ],
    "cln": [
        "5555",
        "2222",
        "2222",
        "2222",
        "2222",
        "2222",
        "2222",
        "090909"
    ],
    "comments": [
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg"
    ],
    "csrfmiddlewaretoken": [
        "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
    ],
    "logs": [
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs"
    ],
    "rpmt_result": [
        "F",
        "F",
        "F",
        "F",
        "F",
        "F",
        "F",
        "F"
    ],
    "rpmt_status": [
        "R",
        "R",
        "R",
        "R",
        "R",
        "R",
        "R",
        "R"
    ]
}

I proposed to change this to add one more level of key(primary key) called product, and all the array values are related to one particular product. So it looks like this!

{
   "product1":{
      "build_date":[
         "2013-10-11"
      ],
      "cln":[
         "23423"
      ],
      "comments":[
         "hghjg"
      ],
      "cbs_merge_blocker":[
         "76767"
      ],
      "rpmt_status":[
         "R"
      ],
      "logs":[
         "no logs"
      ],
      "cbs_other_bugs":[
         "666"
      ],
      "csrfmiddlewaretoken":[
         "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
      ],
      "build_num":[
         "12345"
      ],
      "cbs_waived":[
         "89898"
      ],
      "rpmt_result":[
         "F"
      ]
   },
   "product2":{
      "build_date":[
         "2013-10-11"
      ],
      "cln":[
         "23423"
      ],
      "comments":[
         "hghjg"
      ],
      "cbs_merge_blocker":[
         "76767"
      ],
      "rpmt_status":[
         "R"
      ],
      "logs":[
         "no logs"
      ],
      "cbs_other_bugs":[
         "666"
      ],
      "csrfmiddlewaretoken":[
         "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
      ],
      "build_num":[
         "12345"
      ],
      "cbs_waived":[
         "89898"
      ],
      "rpmt_result":[
         "F"
      ]
   }
}