简体   繁体   English

meal, created = Meal.objects.get_or_create(name=meal_data.get('name', '')) AttributeError: 'str' object 没有属性 'get'

[英]meal, created = Meal.objects.get_or_create(name=meal_data.get('name', '')) AttributeError: 'str' object has no attribute 'get'

this is django rest_framework api I created this api for restourant.这是 django rest_framework api 我为餐厅创建了这个 api。 This is menu api. I want to save menu.json's data to my database, but I could not.这是菜单 api。我想将 menu.json 的数据保存到我的数据库中,但我不能。 Can you give any advice to save json data to my models.你能给我任何建议来将 json 数据保存到我的模型中吗? I got this error:我收到此错误:

      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'

How can I solve it?我该如何解决? I need your help?我需要你的帮助?

models.py模型.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)

This is some data from menu.json not all code include there.这是来自 menu.json 的一些数据,并非所有代码都包含在那里。 menu.json菜单.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"
          }
        ]
      }


first, your menu.json you upload incorrect format.首先,你的menu.json你上传的格式不对。 Missing some character.缺少一些字符。

Assum you have correct json like you upload to variable json_data假设你有正确的 json 就像你上传到变量json_data

Error you show mean meal_data you get is string.你显示的错误意味着你得到的meal_data是字符串。 And it can not use attribute get .而且它不能使用属性get

Try update like this for correct get every meal in json. meal_data become dict and can use 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', ''))

The main thing is that you are not looping correctly through the dictionary (You are trying to access name attribute inside the key value which is a string).最主要的是你没有在字典中正确循环(你试图访问key内的name属性,它是一个字符串)。 You can either:您可以:

def store_json_data(json_data):
    for key, meal_data in json_data.items():
        if key == 'meals':
            for meal in meal_data:
                print(meal.get('name', ''))

        if key == 'ingredients':
            for ingredient in meal_data:
                ...

Or, instead of looping through the dictionary key:value pair , access the list value manually and then loop through it:或者,不是遍历字典key:value pair ,而是手动访问列表值,然后遍历它:

def store_json_data(json_data):
    meals = json_data.get("meals")
    ingredients = json_data["ingredients"]

    for meal in meals:
        print(meal.get('name', ''))

    for ingredient in ingredients:
        ...

That being said, what you are trying to do will not work, since when you makemigrations for the first time, database tables are not yet created and will raise an OperationalError .话虽如此,您尝试执行的操作将行不通,因为当您第一次进行迁移时,数据库表尚未创建并将引发OperationalError

I would suggest usingpost_migrate signal to populate your tables.我建议使用post_migrate信号来填充您的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM