Issue
my input is using JSON post request
{
"rawdata": [
{
"id": "89729999",
"name": "testname",
"product": "testproduct",
"modified_at": "2023-12-14T03:00:00.000Z",
"modified_by": "personname",
"asset": {
"configname": ["testconfig"],
"serialnumber": ["testserialnumber"],
"owner": ["owner1","owner2"]
}
}
]
}
model.py
class Host(models.Model):
id = models.CharField(primary_key=True, max_length=15)
name = models.CharField(max_length=80)
product = models.CharField(max_length=50)
modified_at = models.DateTimeField()
modified_by = models.CharField(max_length=50)
class Hostinfo(models.Model):
fk = models.ForeignKey(Host, on_delete=models.CASCADE) ## id
parameter_section = models.CharField(max_length=40)
parameter = models.CharField(max_length=80)
parameter_index = models.IntegerField() ## index
value = models.CharField(max_length=200, null=True)
modified_at = models.DateTimeField()
modified_by = models.CharField(max_length=50)
view.py
@api_view(('POST',))
def hostrequest(request):
data=request.data.get('rawdata')
print(data)
try:
for item in data:
host=Host()
host.cmdbid = item['id']
host.name = item['name']
host.product =item['product']
host.modified_at= item['modified_at']
host.modified_by= item['modified_by']
host.save()
hostparameter = Hostinfo()
for parameter_section in item:
if parameter_section != "id" and parameter_section != "name" and parameter_section != "product" and parameter_section != "modified_at" and parameter_section != "modified_by":
detailData = item[parameter_section]
for parameter in detailData:
parameters = detailData[parameter]
for parameter_index in parameters:
value = parameters[parameter_index]
hostparameter.fk += item['id']
hostparameter.parameter_section += parameter_section['parameter_section']
hostparameter.parameter += parameter['parameter']
hostparameter.parameter_index += parameter_index['parameter_index']
hostparameter.value += value['value']
hostparameter.save()
response_data={"error":False,"Message":"Updated Successfully"}
return JsonResponse(response_data,safe=False,status=status.HTTP_201_CREATED)
except:
response_data={"error":True,"Message":"Failed to Update Data"}
return JsonResponse(response_data,safe=False)
i executed view script without any issue but i'm only able insert value into host. But not able insert into Hostinfo.
Anyone could help on this why i not able insert into Hostinfo. Any issue on this script on view.py ?
final output on Hostinfo table
fk | parameter_section | parameter | parameter_index | value | modified_at |
---|---|---|---|---|---|
89.. | asset | configname | 0 | testco. | 2023-12-14.. |
89.. | asset | owner | 0 | owner1 | 2023-12-14.. |
89.. | asset | owner | 1 | owner2 | 2023-12-14.. |
Solution
Commented out the line with data because I moved the JSON object outside the view. Used the item['asset'] condition, checking whether it exists, and in a loop I extract key, value
, followed by iterating over value
in a nested loop. Since when using save() I only saved the last record, I used Hostinfo.objects.create
to save (you can also apply it to Host. The data was saved to the database).
def hostrequest(request):
#data = request.data.get('rawdata')
try:
for item in data['rawdata']:
host = Host()
host.id = item['id']
host.name = item['name']
host.product = item['product']
host.modified_at = item['modified_at']
host.modified_by = item['modified_by']
host.save()
r = Host.objects.get(id=item['id'])
if item['asset']:
for key, value in item['asset'].items():
if key != 'serialnumber':
for i in range(len(value)):
Hostinfo.objects.create(fk=r, parameter_section='asset',
parameter=key, parameter_index=i,
value=value[i],
modified_at=item['modified_at'])
response_data = {"error": False, "Message": "Updated Successfully"}
return JsonResponse(response_data, safe=False, status=status.HTTP_201_CREATED)
except:
response_data = {"error": True, "Message": "Failed to Update Data"}
return JsonResponse(response_data, safe=False)
Answered By - inquirer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.