首页 > 解决方案 > TypeError:得到了一个意外的关键字参数“模型”(Django)

问题描述

我正在创建一个数据迁移,目的是为数据库实例化几个 Mineral 对象。python manage.py migrate在控制台中运行时出现错误: TypeError: Mineral() got an unexpected keyword argument 'model'.

我找不到任何类似于 Django 提出的这个关键字的东西。是什么导致了这个阻止创建模型实例的错误?

#models.py

from django.db import models

# Create your models here.
class Mineral(models.Model):
    name = models.CharField(unique=True, max_length=60, null="")
    image_filename = models.CharField(max_length=65, null="")
    image_caption = models.CharField(max_length=410, null="")
    category = models.CharField(max_length=50, null="")
    formula = models.CharField(max_length=280, null="")
    strunz_classification = models.CharField(max_length=110, null="")
    color = models.CharField(max_length=195, null="")
    crystal_system = models.CharField(max_length=130, null="")
    unit_cell = models.CharField(max_length=140, null="")
    crystal_symmetry = models.CharField(max_length=130, null="")
    cleavage = models.CharField(max_length=170, null="")
    mohs_scale_hardness = models.CharField(max_length=70, null="")
    luster = models.CharField(max_length=75, null="")
    streak = models.CharField(max_length=60, null="")
    diaphaneity = models.CharField(max_length=80, null="")
    optical_properties = models.CharField(max_length=85, null="")
    refractive_index = models.CharField(max_length=125, null="")
    crystal_habit = models.CharField(max_length=240, null="")
    specific_gravity = models.CharField(max_length=70, null="") 
    group = models.CharField(max_length=20, null="")

def __str__(self):
    return self.name
# retrieve_json.py

import json

def pull_json(file):
    with open(file, 'r') as json_file:
        data = json.load(json_file)

    return data
from django.db import migrations
import os.path

from ..data.retrieve_json import pull_json

def load_mineral_data(apps, schema_editor):

    Mineral = apps.get_model('minerals', 'Mineral')

    mineral_data = pull_json(os.path.abspath('minerals/data/minerals.json'))

    for m in mineral_data:
        Mineral.objects.create(**m)


class Migration(migrations.Migration):

    dependencies = [
        ('minerals', '0001_define_mineral'),
    ]

    operations = [
        migrations.RunPython(load_mineral_data, migrations.RunPython.noop)
    ]

标签: pythondjango

解决方案


推荐阅读