我最近在使用Python和Django时一直在讨论这个教程 - https://www.codementor.io/jadianes/get-started-with-django-building-recommendation-review-app-du107yb1a
我会在我去的时候改变那个应用程序,只是为了进行实验,但是我对#m; makemigrations'有点停滞不前。
当我运行time.h
时,我得到一个跟踪如下:
python manage.py makemigrations reviews
以下是我的模特
Traceback (most recent call last): File "manage.py", line 15, in
<module>
execute_from_command_line(sys.argv) File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py",
line 371, in execute_from_command_line
utility.execute() File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py",
line 347, in execute
django.setup() File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\site-packages\django\__init__.py",
line 24, in setup
apps.populate(settings.INSTALLED_APPS) File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\registry.py",
line 112, in populate
app_config.import_models() File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\site-packages\django\apps\config.py",
line 198, in import_models
self.models_module = import_module(models_module_name) File "C:\Users\CalPC\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py",
line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File
"<frozen importlib._bootstrap>", line 971, in _find_and_load File
"<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in
exec_module File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed File "C:\Users\CalPC\Desktop\review_app\prodreview\reviews\models.py", line
2, in <module>
import numpy as np ModuleNotFoundError: No module named 'numpy'
我正在运行Django 2.0和python 3.6.5
如果有人能指出我可能会出错的地方,那就太棒了。
提前致谢!
答案 0 :(得分:3)
如果您只是用它来计算平均值,则不需要numpy
。您可以改用statistics.mean
。
import statistics
class Product(models.Model):
name = models.CharField(max_length=200)
def average_rating(self):
all_ratings = map(lambda x: x.rating, self.review_set.all())
return statistics.mean(all_ratings)
您还可以使用aggregate
让数据库计算平均值。
from django.db.models import Avg
def average_rating(self):
return self.review_set.aggregate(avg_rating=Avg('rating'))['avg_rating']
如果您想获得查询集中每个产品的平均值,或annotate
可能很有用。
Product.objects.annotate(avg_rating=Avg('review__rating'))
答案 1 :(得分:0)
确保numpy
在您的需求文件中,然后pip install -r requirements.txt