Save JSON data to sqlite database on Django - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Save JSON data to sqlite database on Django (/thread-36763.html) |
Save JSON data to sqlite database on Django - Quin - Mar-26-2022 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 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: So 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! |