sqlite 创建的餐食=餐食.对象.get_or_create(名称=餐食_数据.get('name ',''))属性错误:“str”对象没有属性“get”

zpqajqem  于 2023-01-31  发布在  SQLite
关注(0)|答案(1)|浏览(171)

这是django rest_framework API我为restourant创建了这个api.这是menu api .我想把menu.json的数据保存到我的数据库,但是我不能.你能给予我一些建议把json数据保存到我的模型吗?

File "C:\Users\OSMAN MERT\Desktop\menu\menu_core\api\models.py", line 36, in store_json_data
        meal, created = Meal.objects.get_or_create(name=meal_data.get('name', ''))
    AttributeError: 'str' object has no attribute 'get'

我该怎么解决?我需要你的帮助?
models.py

from django.db import models
    import json
    # Create your models here.
    
    class Meal(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=100)
        is_vegetarian = models.BooleanField(default=False)
        is_vegan = models.BooleanField(default=False)
    
    class Ingredient(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=100)
        groups = models.CharField(max_length=100)
    
    class Option(models.Model):
        id = models.AutoField(primary_key=True)
        ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)
        quality = models.CharField(max_length=100)
        price = models.FloatField()
        per_amount = models.CharField(max_length=100)
    
    class MealIngredient(models.Model):
        id = models.AutoField(primary_key=True)
        meal = models.ForeignKey(Meal, on_delete=models.CASCADE)
        ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
    
    
    
    
    path = r"C:\Users\OSMAN MERT\Desktop\menu\menu_core\menu.json"
    
    def store_json_data(json_data):
        for meal_data in json_data:
            meal, created = Meal.objects.get_or_create(name=meal_data.get('name', ''))
            if not created:
                meal.is_vegetarian = meal_data.get('is_vegetarian', False)
                meal.is_vegan = meal_data.get('is_vegan', False)
                meal.save()
            for ingredient_data in meal_data.get('ingredients', []):
                ingredient, _ = Ingredient.objects.get_or_create(name=ingredient_data.get('name', ''), meal=meal)
                for option_data in ingredient_data.get('options', []):
                    option, _ = Option.objects.get_or_create(quality=option_data.get('quality', ''), ingredient=ingredient)
                    option.price = option_data.get('price', 0)
                    option.save()
    
    def load_json_file(path):
        with open(path) as json_file:
            json_data = json.load(json_file)
            store_json_data(json_data)
    
    load_json_file(path)

这是来自menu.json的一些数据,并非所有代码都包含在其中。menu.json

{
    "meals": [
      {
        "id": 1,
        "name": "Rice and chicken bowl",
        "ingredients": [
          { "name": "Rice", "quantity": 120, "quantity_type": "gram" },
          { "name": "Chicken", "quantity": 85, "quantity_type": "gram" }
        ]
      },
      {
        "id": 2,
        "name": "Pasta with marinara sauce and vegetables",
        "ingredients": [
          { "name": "Pasta", "quantity": 115, "quantity_type": "gram" },
          {
            "name": "Marinara sauce",
            "quantity": 120,
            "quantity_type": "millilitre"
          },
          { "name": "Vegetables", "quantity": 240, "quantity_type": "gram" }
        ]
      },
      {
        "id": 3,
        "name": "Grilled chicken with roasted vegetables",
        "ingredients": [
          { "name": "Chicken", "quantity": 85, "quantity_type": "gram" },
          { "name": "Vegetables", "quantity": 240, "quantity_type": "gram" }
        ]
      },
      {
        "id": 4,
        "name": "Beef stir-fry with rice",
        "ingredients": [
          { "name": "Beef", "quantity": 115, "quantity_type": "gram" },
          { "name": "Rice", "quantity": 120, "quantity_type": "gram" },
          { "name": "Vegetables", "quantity": 240, "quantity_type": "gram" }
        ]
      },
      {
        "id": 5,
        "name": "Pork chops with mashed potatoes and gravy",
        "ingredients": [
          { "name": "Pork chops", "quantity": 115, "quantity_type": "gram" },
          {
            "name": "Mashed potatoes",
            "quantity": 120,
            "quantity_type": "gram"
          },
          { "name": "Gravy", "quantity": 120, "quantity_type": "millilitre" }
        ]
      },
      {
        "id": 6,
        "name": "Grilled salmon with roasted asparagus",
        "ingredients": [
          { "name": "Salmon", "quantity": 85, "quantity_type": "gram" },
          { "name": "Asparagus", "quantity": 240, "quantity_type": "gram" }
        ]
      },
      {
        "id": 7,
        "name": "Shrimp scampi with linguine",
        "ingredients": [
          { "name": "Shrimp", "quantity": 115, "quantity_type": "gram" },
          { "name": "Linguine", "quantity": 115, "quantity_type": "gram" },
          { "name": "Butter", "quantity": 10, "quantity_type": "millilitre" },
          { "name": "Garlic", "quantity": 10, "quantity_type": "gram" },
          { "name": "White wine", "quantity": 60, "quantity_type": "millilitre" }
        ]
      },
      {
        "id": 8,
        "name": "Vegetarian stir-fry with tofu",
        "ingredients": [
          { "name": "Tofu", "quantity": 115, "quantity_type": "gram" },
          { "name": "Rice", "quantity": 120, "quantity_type": "gram" },
          { "name": "Vegetables", "quantity": 240, "quantity_type": "gram" }
        ]
      },
      {
        "id": 9,
        "name": "Fruit salad with mixed berries and yogurt",
        "ingredients": [
          { "name": "Mixed berries", "quantity": 240, "quantity_type": "gram" },
          { "name": "Yogurt", "quantity": 120, "quantity_type": "millilitre" }
        ]
      }
    ],
  
    "ingredients": [
      {
        "name": "Rice",
        "groups": ["vegan", "vegetarian"],
        "options": [
          {
            "name": "Long grain white rice",
            "quality": "high",
            "price": 3,
            "per_amount": "kilogram"
          },
          {
            "name": "Medium grain brown rice",
            "quality": "medium",
            "price": 2,
            "per_amount": "kilogram"
          },
          {
            "name": "Quick cooking white rice",
            "quality": "low",
            "price": 1.5,
            "per_amount": "kilogram"
          }
        ]
      }
mpbci0fu

mpbci0fu1#

首先,你上传的menu.json不正确,缺少一些字符。
假设您有正确的json,就像您上传到变量json_data一样
你显示的错误意味着你得到的meal_data是字符串。并且它不能使用属性get
尝试这样更新正确获取每餐在json. meal_data成为dict,可以使用get

for meal_data in json_data.get("meals"):
        meal, created = Meal.objects.get_or_create(name=meal_data.get('name', ''))

相关问题