模块 'django.db.models' 没有属性 'Foreignkey'



我是Django的新手,我刚刚开始使用Djaango示例学习它,但我收到错误消息。这是我的回溯

C:UsersHarsleyshop>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libsite-packagesdjangocoremanagement__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libsite-packagesdjangocoremanagement__init__.py", line 328, in execute
    django.setup()
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libsite-packagesdjango__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libsite-packagesdjangoappsregistry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libsite-packagesdjangoappsconfig.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:UsersHarsleyAppDataLocalProgramsPythonPython36-32libimportlib__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:UsersHarsleyshopshopsmodels.py", line 16, in <module>
    class Product(models.Model):
  File "C:UsersHarsleyshopshopsmodels.py", line 17, in Product
    category = models.Foreignkey(Category,related_name='products')
AttributeError: module 'django.db.models' has no attribute 'Foreignkey'

这是模型文件,我已经浏览了它,似乎我没有犯任何错误。

from django.db import models
# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, unique = True)
    class Meta:
        ordering = ('name',)
        verbose_name = 'category'
        verbose_name_plural = 'categories'
        def ___str__(self):
            return self.name
class Product(models.Model):
    category = models.Foreignkey(Category,related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField()
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),) 
    def __str__(self):
        return self.name

你在代码中拼错了ForeignKeyk应该是大写的。

class Product(models.Model):
    category = models.ForeignKey(Category,related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField()
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),) 
    def __str__(self):
        return self.name

最新更新