gpt4 book ai didi

python - 名称错误 : name 'Usuario' is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:11 25 4
gpt4 key购买 nike

当我尝试执行“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com