我有一个Django项目,我的模型,视图和URL已设置完毕。路径结构如下:
--mainProject
--measurements
--views.py
--urls.py
--models.py
--migrations
--templates.py
--management
--commands
--add_data.py
--clear_data.py
--init.py
--_pycache_
构建此项目时,我首先创建了models.py,然后进行了迁移。之后,我迁移了。 add_data.py文件将数据添加到表中,clear_data.py文件删除数据。我运行了add_data.py文件并且它运行了一次,但是我遇到了类似的错误。我忘记了如何通过命令行运行它。但是,现在如果我删除数据库并尝试运行add_data.py文件,我会收到此错误:
来自measurements.models导入区域,类别,位置,测量 ModuleNotFoundError:没有名为'measurements'的模块
这是我的代码:
models.py
from django.db import models
from django.db.models import Avg
class Area(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200)
longitude = models.FloatField()
latitude = models.FloatField()
def number_of_locations(self):
return len(self.location_set.all())
# Function to get the average measurements of locations in area
def average_measurement(self):
return Measurement.objects.filter(location__area=self).aggregate(Avg('value'))['value__avg']
# Get all category names for area
def category_names(self):
categories = self.category_set.all()
cList = ""
for category in categories:
cList+= category.name + ", "
return cList[:-2]
def __str__(self):
return self.name
class Location(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200)
altitude = models.IntegerField(verbose_name="Altitude in feet")
area = models.ForeignKey(Area, on_delete=models.CASCADE)
def __str__(self):
return self.area.name + ":" + self.name
class Measurement(models.Model):
id = models.IntegerField(primary_key=True)
value = models.FloatField()
date = models.DateTimeField()
location = models.ForeignKey(Location, on_delete=models.CASCADE)
def __str__(self):
return "measurement@" + self.location.__str__()
class Category(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=140)
description = models.CharField(max_length=250)
members = models.ManyToManyField(Area)
def __str__(self):
return self.name
add_data.py
from measurements.models import Area, Category, Location, Measurement
import random
from django.core.management.base import BaseCommand
from django.utils import timezone
class Command(BaseCommand):
help = "adds sample entities to the application"
def handle(self, *args, **options):
area_data = [
[1, "Grand Canyon", 20, 30],
[2, "Boca Raton", 40, 50],
[3, "Atlanta", 60, 70],
[4, "Mount Hood", 80, 90],
[5, "Mount Rainer", 46.5, 121.5],
[6, "Saint Olaf", 44, 93],
[7, "Mount St. Helens", 46, 122]
]
for ad in area_data:
a = Area(name=ad[1], id=ad[0], longitude=ad[2], latitude=ad[3])
a.save()
location_data = [
[11, "South rim", 200, 1],
[12, "North rim", 300, 1],
[13, "Phantom Ranch", 100, 1],
[14, "Waterfront", 10, 2],
[15, "Town center", 15, 2],
[16, "Town center", 310, 3],
[17, "University", 320, 3],
[18, "Mall", 330, 3],
[19, "Airport", 350, 3],
[20, "South pass", 3500, 4],
[21, "North rim", 4500, 4],
[22, "Crater", 4000, 4],
[23, "North pass", 14000, 5],
[24, "South glacier", 15000, 5],
[25, "Ranger station", 16000, 5],
[26, "Summit", 17000, 5],
[27, "Yttrboe", 250, 6],
[28, "Science", 250, 6],
]
no_measurements_location_ids = [23]
for locd in location_data:
loc = Location(id=locd[0], name=locd[1], altitude=locd[2])
a = Area.objects.get(pk=locd[3])
loc.area = a
loc.save()
category_data = [
[31, "Volcanos", "Areas that are on volcanoes", [4, 5]],
[32, "East", "Areas that are in the east", [2, 3]],
[33, "West", "Areas that are in the west", [1, 4, 5]],
[34, "Wetlands", "Areas that are wetlands", []]
]
for catd in category_data:
cat = Category(id=catd[0], name=catd[1], description=catd[2])
cat.save()
for memb_id in catd[3]:
a = Area.objects.get(pk=memb_id)
cat.members.add(a)
cat.save()
num_meas_per_loc = 10
for locd in location_data:
loc_id = locd[0]
if loc_id not in no_measurements_location_ids:
loc = Location.objects.get(pk=loc_id)
for meas_id in range(loc_id*100, loc_id*100+num_meas_per_loc):
val = random.uniform(5+loc_id*3, 15+loc_id*3)
meas = Measurement(value=val, id=meas_id, date=timezone.now())
meas.location = loc
meas.save()
我做错了什么导致了这个错误?
答案 0 :(得分:1)
add_data.py
是一个管理命令,而不是一个独立的脚本,所以你应该运行它:
python manage.py add_data
如果直接运行add_data.py
,那么您的主项目目录(包含manage.py
的目录)将不会自动进入Python路径,因此您将收到导入错误。