python - 命令对象没有meta属性,Django管理命令
问题描述
我正在尝试运行一次性管理命令来预填充数据库。
这是模型:
class ZipCode(models.Model):
zip_code = models.CharField(max_length=7)
latitude = models.DecimalField(decimal_places=6, max_digits =12)
longitude = models.DecimalField(decimal_places=6, max_digits =12)
这是管理命令:
from django.core.management.base import BaseCommand
from core.models import ZipCode
class Command(BaseCommand):
def populate_db(self):
zip_code_object = ZipCode(zip_code='10566', latitude = 10.2, longitude= 43.4) #test data to see if I could get it working
ZipCode.save(self)
def handle(self, *args, **options):
self.populate_db()
但是当我运行python3.6 manage.py populate_zip_code_db
它返回
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
for field in self._meta.concrete_fields:
AttributeError: 'Command' object has no attribute '_meta'
有谁知道如何让这个工作?我刚刚发现了管理命令,所以我一无所知。
解决方案
而不是ZipCode.save(self)
它应该是zip_code_object.save()
:
class Command(BaseCommand):
def populate_db(self):
zip_code_object = ZipCode(zip_code='10566', latitude = 10.2, longitude= 43.4) #test data to see if I could get it working
zip_code_object.save()
def handle(self, *args, **options):
self.populate_db()
推荐阅读
- java - BouncyCastle 如何生成 ECDH“密钥”?
- python - 实现和运行 3D U-net 以对合成数据进行去噪
- python - 通过 cron 和 refresh_and_apply_credential 导入 BigQuery 获取“NoneType”对象没有“应用”属性
- excel - 如何打开 .msg 文件?
- python - 我该如何解决这个特定的“TypeError:类型'NoneType'无法序列化。” 错误?
- drake - 我已经安装了 drake 并使用 Matlab 设置了 drake,现在该怎么办?
- javascript - 大范围输入的素数总和
- angular - 带有选择的属性的角度 i18n
- javascript - 单击链接时关闭移动设备上的菜单
- docker - 我是否在容器上运行?