当我尝试执行“makemigrations”时,我在 django 中遇到此错误
代码:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Usuario(models.Model):
user = models.OneToOneField(User)
amigos = models.ManyToManyField(Amigo)
class Amigo(models.Model):
usuarios = models.ForeignKey(Usuario)
class Regalo(models.Model):
nombre = models.CharField(max_length=100)
enlace_compra = models.CharField(max_length=200)
amigo_encargado = models.OneToOneField(User)
抱歉,我的英语很差,我使用翻译。
最佳答案
Amigo
类在声明 Usuario
类时未定义,因此您不能在 amigos 字段中使用它。
来自 django 文档:
If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself
在 Usuario
类中更改这一行:
amigos = models.ManyToManyField(Amigo)
至:
amigos = models.ManyToManyField('Amigo')
关于python - 名称错误 : name 'Usuario' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28940744/