Hello everyone
I am trying to save JSON data to sqlite database on Django
I have these small code chunks here which are giving me some problems.
Here is a chunk of my
Now for testing purposes I put
I tried doing this without the
Thank you in advance!
I am trying to save JSON data to sqlite database on Django
I have these small code chunks here which are giving me some problems.
Here is a chunk of my
models.pyfile
class Recipes(models.Model):
name = models.CharField(max_length=120, default='')
pub_date = models.DateTimeField('date published')
style = models.CharField(max_length=200, default='')
brewer = models.CharField(max_length=100, default='')
type = models.CharField(max_length=20, default='All Grain')
version = models.CharField(max_length=20, default='1')
batch_size = models.DecimalField(decimal_places=2, max_digits=8, default=0.0)
boil_size = models.DecimalField(decimal_places=2, max_digits=8, default=0.0)
boil_time = models.DecimalField(decimal_places=1, max_digits=4, default=0.0)
efficiency = models.DecimalField(decimal_places=1, max_digits=4, default=75.0)
ibu = models.DecimalField(decimal_places=1, max_digits=4, default=0.0)
abv = models.DecimalField(decimal_places=2, max_digits=4, default=0.0)
notes = models.TextField(default='')
carbonation = models.DecimalField(decimal_places=2, max_digits=4, default=0.0)
primary_age = models.DecimalField(decimal_places=1, max_digits=4, default = 0)
secondary_age = models.DecimalField(decimal_places=1, max_digits=4, default = 0)
age = models.DecimalField(decimal_places=1, max_digits=4, default = 0)
__fermentables = []
@classmethod
def create(cls,attr):
recipe = cls()
# do something with the book
for k in Recipes._meta.fields:
if k.name in attr:
setattr(recipe,k.name,attr[k.name])
return recipeHere is the part of views.pywhich is giving me trouble
def saveRecipe(request):
try:
data=json.loads(request.read())
print("printing values")
print(data["name"]) #prints here works
recipe = Recipes.create(attr=data)
recipe.name = data["name"]
recipe.save()
recipe.addYeast(items=data["yeast"])
recipe.addFermentables(items=data["fermentables"])
recipe.addHops(items=data["hops"])
recipe.addMashStep(items=data["mash"])
return HttpResponse(serialize('json', [recipe]), content_type='application/json')
except:
return HttpResponse("error")Basically I have a button which parses JSON from filled forms and when I print name print(data["name"])it seems to be parsed correctly.
Now for testing purposes I put
recipe.save()in the part of the views file where you can see and I think that it is technically supposed to save the parsed information into the database but when I check the database there is nothing there.
I tried doing this without the
try/exceptblock, however then I get this error:
Error:Internal Server Error: /save-recipe
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Admin\AppData\Roaming\Python\Python310\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Admin\Desktop\praktika\brew\brewery\views.py", line 106, in saveRecipe
recipe = Recipes.create(attr=data)
File "C:\Users\Admin\Desktop\praktika\brew\brewery\models.py", line 73, in create
print(name, style)
NameError: name 'name' is not defined
[26/Mar/2022 19:30:21] "POST /save-recipe HTTP/1.1" 500 67750So basically my question is why is the recipe.save()not doing what it's supposed to and what is missing in order to save the data correctly?
Thank you in advance!
