モデル、ビュー、およびURLを設定したDjangoプロジェクトがあります。ModuleNotFoundErrorしかし、モジュールへのパスが正しいです。
--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ファイルを実行しようとした場合しかし、今、私はこのエラーを取得する:
from measurements.models import Area, Category, Location, Measurement ModuleNotFoundError: No module named '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 "[email protected]" + 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()
このエラーが発生する原因は何ですか?
あなたのプロジェクトのルートフォルダとアプリは 'M'キャップで始まっていますか? –
@RajaSimonいいえ – user2896120
あなたは '__init __。py'ファイルを表示していません(誤った' init.py'を除く) – Alasdair