'Django JSON not being saved in sqlite database

I am just testing some stuff to figure out how Django works in general.

Now I have these small code chunks here which are giving me some problems.

Here is a chunk of my models.py file

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 recipe

Here is the part of views.py which 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 I 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.

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?

For the purpose of simplicity let's ignore everything that comes after the save fuction.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source