Cannot Load Dhango Fixtures into database - django

I am able to load 1 fixture file into the database, but not the 2nd file. I run the command python manage.py loaddata fixture.json, and I get the error. I'm not sure how to solve this serialization error, any insights appreciated.
The error:
for obj in objects:
File "/Users/ayunas/.local/share/virtualenvs/CS24-BW-MUD-lJVJyZQx/lib/python3.7/site-packages/django/core/serializers/json.py", line 73, in Deserializer
raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/ayunas/Documents/lambda/CS24-BW-MUD/tower_app/fixtures/test_fixture.json':
Here is how the fixture file looks like:
[
{
"model": "tower_app.item",
"pk": 1,
"fields": {
"item_name": "Black Stone",
"description": "a white stone from Paradise, blackened by the sins of mankind.",
"strength": 10,
"item_type": "weapon",
"playerID" : null,
"roomID" : 4
}
}
]

Related

Getting error of PipelineActivity must have one and only one member when using Boto3 and Create_Pipeline

I have a python program that is using boto3 to create an IoT Analytics path. My program was able to successfully create the channel and the datastore but fails when I try to connect the two through the create pipeline function. My code is as follows:
dactivity = [{
"channel": {
"channelName": channel["channelName"],
"name": IoTAConfig["channelName"],
"next" : IoTAConfig["datastoreName"]
},
"datastore": {
"datastoreName": ds["datastoreName"],
"name": IoTAConfig["datastoreName"]
}
}]
pipeline = iota.create_pipeline(
pipelineActivities = dactivity,
pipelineName = IoTAConfig["pipelineName"]
)
The error code is as follows:
Traceback (most recent call last):
File "createFullGG.py", line 478, in <module>
createIoTA()
File "createFullGG.py", line 268, in createIoTA
pipelineName = IoTAConfig["pipelineName"]
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 623, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) when calling the UpdatePipeline operation: PipelineActivity must have one and only one member
According to the documentation pipeline activities can contain from 1 to 25 entries as long as they are in an array of 1 object. I have no idea why this continues to fail. Any help is appreciated.
The public documentation looks a little confusing because of the way that optional elements are represented, the good news is that this is an easy fix.
A corrected version of what you are trying would be written as;
dactivity=[
{
"channel": {
"channelName": channel["channelName"],
"name": IoTAConfig["channelName"],
"next" : IoTAConfig["datastoreName"]
}
},
{
"datastore": {
"datastoreName": ds["datastoreName"],
"name": IoTAConfig["datastoreName"]
}
}
]
response = client.create_pipeline(
pipelineActivities = dactivity,
pipelineName = IoTAConfig["pipelineName"]
)
So it's an array of activities that you are providing, like [ {A1},{A2} ] if that makes sense?
Does that help?

json.loads() giving "ValueError: Expecting property name:"

I'm trying to convert sttr data type to dict in python using json.loads() function.
But I'm getting the error as:
File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/l`enter code here`ocal/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
In comments you gave the following example of the text you are trying to load as JSON:
[
{
'entity_class': 'Hardware Entities (AHV)',
'status': 'recommended',
'uuid': 'de74178a-cbc7-4c69-ae2f-9e7042bf8e98',
'zprotobuf': 'eNolzD1LAzEYB/DFScHBRXAKoUMr5khiLi/dCgpdCg7qIiLPJU/KQS4nyeEL6ne3p+vv/3K8CGiUMBaY77xhymvHAGVkDg1XsosWnT1bcI42tqZlwbWOqWAis5oD08J3VinOOwMXp',
'version': '2.4-1542668003',
'dependencies': '[{"entity_class": "Dell Update Manager", "version": "1.8-0.112", "exact": "false", "entity_model": "PT Agent on AHV (el6)"}]',
'entity_uuid': '00e8f575-d959-4d7f-860a-61cb84400b7a',
'order': 4,
}
]
JSON should use double quotes, not single quotes. The above looks like native python data struct syntax. Here is the above dumped from native python to JSON.
import json
native_python_data = [
{
'entity_class': 'Hardware Entities (AHV)',
'status': 'recommended',
'uuid': 'de74178a-cbc7-4c69-ae2f-9e7042bf8e98',
'zprotobuf': 'eNolzD1LAzEYB/DFScHBRXAKoUMr5khiLi/dCgpdCg7qIiLPJU/KQS4nyeEL6ne3p+vv/3K8CGiUMBaY77xhymvHAGVkDg1XsosWnT1bcI42tqZlwbWOqWAis5oD08J3VinOOwMXp',
'version': '2.4-1542668003',
'dependencies': [
{
"entity_class": "Dell Update Manager",
"version": "1.8-0.112",
"exact": "false",
"entity_model": "PT Agent on AHV (el6)"
}
],
'entity_uuid': '00e8f575-d959-4d7f-860a-61cb84400b7a',
'order': 4,
}
]
json_string_with_escaping = json.dumps(js, indent=4)
print json_string_with_escaping

json proper encoding and decoding not working with flask, python

I'm taking a text-input from html being json object and trying to work upon that. But when I'm trying the following code, I'm getting error/(page not rendering) in encoding and decoding JSON.
#app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
#getting text-input as text = {'a':'1','b':'2'}
json_input = json.dumps(text)
ordered_json = json.loads(text, object_pairs_hook=ordereddict.OrderedDict)
print ordered_json
processed_text = htmlConvertor(ordered_json)
#rep(jso)
return render_template("my-form.html",processed_text=processed_text)
But when I'm tying to do so with a local JSON variable as jso everything working fine. The same input when I provide with html-input, it's giving an error and I can;t even see the error except displaying Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
#app.route('/', methods=['POST'])
def my_form_post():
jso = '''{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}'''
json_input = json.dumps(jso)
ordered_json = json.loads(jso, object_pairs_hook=ordereddict.OrderedDict)
print ordered_json
processed_text = htmlConvertor(ordered_json)
#rep(jso)
return render_template("my-form.html",processed_text=processed_text)
UPDATE:
Everything working fine now, but for the integers, it isn't working.
For eg:
{"name":"yo","price":"250"}
works perfectly but
{"name":"yo","price":250}
ain't.
What's the solution for that? Any specific answer or I would have to check for integer in python and then convert it to string before applying any JSON related methods and functioning.
Not sure if this is your problem, but {'a':'1','b':'2'} is not a valid JSON object because of the single quotes:
>>> json.loads("{'a':'1','b':'2'}")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 171, in JSONObject
raise ValueError(errmsg("Expecting property name", s, end))
ValueError: Expecting property name: line 1 column 1 (char 1)
If instead you use double quotes everything works fine:
>>> json.loads("{\"a\":\"1\",\"b\":\"2\"}")
{u'a': u'1', u'b': u'2'}
Also note that to get stack traces intead of code 500 errors when there is an exception you have to start your flask server as follows:
app.run(debug = True)

Using UTF-8 encoded JSON fixture file in Django

I'm trying to write a JSON initial data fixture that will be loaded after every call to syncdb.
I placed an initial_data.json file in my mysite/myapp/fixtures directory:
[
{
"model": "myapp.Person",
"pk": 1,
"fields": {
"first_name": "Tom",
"last_name": "Yam"
}
}
]
Everything is working when the file is encoded in ASCII, but when I save it in UTF-8 encoding (I need to use non-ASCII characters) I get to following error:
Problem installing fixture 'initial_data.json': Traceback (most recent call last):
File "D:\Tom\DjangoEnv\Lib\site-packages\django\core\management\commands\loaddata.py", line 190, in handle
for obj in objects:
File "D:\Tom\DjangoEnv\Lib\site-packages\django\core\serializers\json.py", line 47, in Deserializer
raise DeserializationError(e)
DeserializationError: No JSON object could be decoded
According to the Django documentation, I need to set ensure_ascii=False when working with non-ASCII data and JSON serializers, but I can't figure how to do it (since its being called from the syncdb function.
Any ideas how to use a UTF-8 encoded JASON file as a fixture?
load_data would not pass ensure_ascii option to serializer so you have two options:
convert data to ascii unicode escaped before loading it, ie:
import codecs
encoded = codecs.open('/tmp/tst.txt', 'r', 'utf-8').read().encode(
'ascii', 'backslashreplace')
open('/tmp/tst-encoded.txt', 'w').write(encoded)
write your own management command that would pass ensure_ascii
hope this helps.

How to use fixtures in django?

I've a very simple app and I've created a fixture. A folder named as fixtures and a file named as initial_data.json. Following is the code I've in my initial_data.json file:
[
{
"model": "myapp.model_in_lower_case",
"pk": 1,
"fields": {
"title": "my Title",
"description": "Description goes here..."
}
}
]
But when I run the syncdb command, it says zero fixtures found and the data is not being saved. What's missing?
Make sure you are using manage.py /dumpdata to export the fixture.
save the fixuture under your app fixtures directory not your project directory.
name it initial_data.json and it should work for you.
when the json file does not fit to your database or is invalid, manage.py will throw an exception. I am positive that currently you didnt put the json file in the right place.