python - 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)
]
解决方案
推荐阅读
- nativescript - 在 NativeScript 中创建可缩放标签
- python - awk:致命:无法打开文件“文件”进行读取(权限被拒绝)
- sql - 列中的逗号分隔天名称
- if-statement - 通过改变工作日期来消除周末日期
- java - 为什么hadoop不能加载类?
- linux - 重新安装linux后如何恢复已删除的文件
- react-native - 我想在一个特殊孩子的新闻发布会上改变一个以上孩子的背景
- rest - Spring REST Docs - 由于输入结束,没有要映射的内容
- php - 安装模块后 composer.json 文件出错
- c# - 如何让我的 TwoSum 方法处理无序的数字?