0

I have been messing around with Python and Django recently, following this tutorial - https://www.codementor.io/jadianes/get-started-with-django-building-recommendation-review-app-du107yb1a

I will be altering that app as I go, just to experiment, however I am at a bit of a standstill with 'makemigrations'.

When I run python manage.py makemigrations reviews I get a traceback as follows:

 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'

Here are my Models

from django.db import models
import numpy as np


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 np.mean(all_ratings)

    def __unicode__(self):
        return self.name

class Review(models.Model):
    RATING_CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
    )
    product = models.ForeignKey(Product)
    pub_date = models.DateTimeField('date published')
    user_name = models.CharField(max_length=100)
    comment = models.CharField(max_length=200)
    rating = models.IntegerField(choices=RATING_CHOICES)

I'm running Django 2.0 and python 3.6.5

If anyone could point out where I could be going wrong that would be great.

Thanks in advance!

2
  • 2
    line 2, in import numpy as np ModuleNotFoundError: No module named 'numpy' It seems like you forgot to install a dependency Commented Apr 27, 2018 at 14:06
  • when you activated your env hit pip install numpy and add it to your installed_apps in settings.py Commented Apr 27, 2018 at 14:08

2 Answers 2

3

You don't need numpy if you are only using it to calculate the mean. You could use statistics.mean instead.

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)

You can also use aggregate to get the database to calculate the average.

from django.db.models import Avg

def average_rating(self):
    return self.review_set.aggregate(avg_rating=Avg('rating'))['avg_rating']

Or annotate can be useful if you want to get the average for every product in the queryset.

Product.objects.annotate(avg_rating=Avg('review__rating'))
Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful! Thank you.
0

Make sure numpy is in your requirements file, and then pip install -r requirements.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.