【问题标题】:from django.db import models in models.py but in command prompt i got NameError: name 'model' is not definedfrom django.db import models in models.py 但在命令提示符下我得到 NameError: name 'model' is not defined
【发布时间】:2021-01-29 00:55:57
【问题描述】:

我收到了这个错误: 文件“C:\Users\jiri.svab\Documents\Pipenv\Django\DennisIvy\ecommerce\store\models.py”,第 15 行,在 类产品(模型。模型): 文件“C:\Users\jiri.svab\Documents\Pipev\Django\DennisIvy\ecommerce\store\models.py”,第 16 行,在产品中 name = model.CharField(max_length=200, null=True) NameError:名称“模型”未定义

但在第一行的“models.py”中,我已经导入了模型:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.name

请问有什么线索吗?

谢谢

【问题讨论】:

    标签: django import model pipenv


    【解决方案1】:

    您尚未显示 models.py 文件的其余部分,但它似乎是您的 Product 模型类定义中的错字。在name = model.CharField(max_length=200, null=True) 行中,将model 更改为models。

    【讨论】:

    • 哦,是的,你是对的,我昨天实际上为自己发帖并回答了,但不知何故它没有保存在这里。再次写下这个问题帮助我意识到了一个打字错误。无论如何谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多