使用Django-multiselectfield时出错

时间:2019-01-12 04:28:19

标签: python django

在我的django应用程序中使用multiselectfield程序包并运行makemigrations时,出现以下错误:

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/Users/pc/Desktop/code 2 copy/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
ImportError: No module named 'multiselectfield'

我已经按照doc上的所有说明进行操作,但仍然不知道问题出在哪里。

models.py

from multiselectfield import MultiSelectField

class Meal(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.PROTECT)
category = models.ForeignKey(MealCategory, on_delete=models.PROTECT)
name = models.CharField(max_length=500)
short_description = models.CharField(max_length=500)
image = models.ImageField(upload_to='meal_images/', blank=False)
price = models.IntegerField(default=0)
extras = MultiSelectField(choices = foodtaskerapp.Extra().get_extras())
def __str__(self):
    return self.name


class Extra(models.Model):
extra_n = models.CharField(max_length=200)
meal = models.ForeignKey(Meal, on_delete = models.CASCADE, null=True, blank=True)
extra_price = models.IntegerField(default=0)

@property
def is_extra(self):
    return meal.extras == self

def __str__(self):
    return self.extra_n

def get_extras(self):
    return self.extra_n

1 个答案:

答案 0 :(得分:1)

  1. 您是否通过PIP安装了它?

$ pip install django-multiselectfield

  1. 请确保您正确导入了multiselectfield:

from multiselectfield import MultiSelectField

  1. 您在模型中正确引用了吗?

my_field = MultiSelectField(choices=MY_CHOICES)

有关更多信息,请参见https://pypi.org/project/django-multiselectfield/

如果这不起作用,我很想从您的代码中了解更多。