英文:
The class name with or in a model field to have key
问题
以下是翻译好的部分:
在模型字段中,类名带引号和不带引号之间有什么区别?
在模型字段中,应该使用带引号还是不带引号的类名来建立外键关系?
英文:
I can set with or to as shown below:
class Category(models.Model):
name = models.CharField(max_length=50)
class Product(models.Model): # Here
category = models.ForeignKey("Category", on_delete=models.CASCADE)
name = models.CharField(max_length=50)
Or:
class Category(models.Model):
name = models.CharField(max_length=50)
class Product(models.Model): # Here
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
And, I know that I can set with to () class is as shown below:
class Product(models.Model): # Here
category = models.ForeignKey("Category", on_delete=models.CASCADE)
name = models.CharField(max_length=50)
class Category(models.Model):
name = models.CharField(max_length=50)
And, I know that I set to () class is as shown below:
class Product(models.Model): # Error
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
class Category(models.Model):
name = models.CharField(max_length=50)
Then, I got the error below:
> : name ” is not
My :
What is the the class name with or in a model field to have key ?
Which I use, the class name with or in a model field to have key ?
答案1
得分: 1
以下是要翻译的内容:
What is the the class name with or in a model field to have key ?
在模型字段中,使用带引号和不带引号的类名之间有什么区别?
You can not a that is . , you can not use:
你不能在变量被定义之前引用该变量。实际上,你不能使用:
wrong错误
print(a)
a = 42
at that time, 42 is not . The same holds for , as you found out. You can not refer to a class that is not yet. This is a of as “first-class “: a class is just an . It does not some steps.
因为在那个时候,42 还没有定义。对于类来说也是一样的,正如你所发现的那样。你不能引用一个尚未定义的类。这是将类视为“一等公民”的结果:类只是一个对象。它不需要特殊的编译步骤。
This thus means that you can not refer to a model that has to be in the rest of the code. are where two would each other, and thus in an .
因此,这意味着你不能引用在代码的其余部分中定义的模型。另一个问题是循环导入,其中两个模型会彼此导入,从而导致。
In order to fix this, to the model as a when you refer to it. you this as ”.” with the name of the app where the model is . If you refer to a model in the same app as the one you are , you can omit the .
为了解决这个问题,允许在引用模型时将模型指定为字符串字面值。通常你会将它标记为”.”,其中**是定义模型的应用程序的名称。如果你引用的模型与你正在定义的模型在同一个应用程序中,你可以省略字符串字面值。
will thus, after all the are , try to the and the in the model with a to the real model class.
因此,在加载所有模型之后,将尝试解析字符串字面值,并将模型对象中的字符串字面值替换为对真实模型类的引用。
Which I use, the class name with or in a model field to have key ?
我应该在模型字段中使用带引号还是不带引号的类名来创建外键关系?
Both will , after that the are , in the same and the same hold, so there is no .
两者最终在加载模型后将得到相同的建模结果,并且引用也是相同的,因此没有区别。
If you refer to that will be later in the file, or in that would in , then we don’t have much , then the is the way to “knot the tie”.
如果你引用的模型将在文件后面定义,或者在可能导致循环导入的模块中定义,那么我们没有太多选择,字符串字面值是”打个结”的方式。
An of using the and not the is that most IDEs the and if you to a class, it the uses. IDEs can also see if you refer to an not in scope. with the also “” the and thus can do , but that is only they such logic in the IDE.
使用标识符而不是字符串字面值的优点是,大多数集成开发环境会跟踪标识符,如果你决定重命名一个类,它会自动重命名引用。集成开发环境还可以看到你是否引用了超出范围的标识符。带有扩展的也“理解”字符串字面值表示法,因此可以执行等效的操作,但这仅仅是因为他们在IDE中实现了这样的逻辑。
: Using an is thus than a if that is . If you use later in the file, or you would , you have to use a .
结论:如果可能的话,使用标识符要略微好于使用字符串字面值。如果你在文件后面使用了后定义的模型,或者引入了循环导入,那么你必须使用字符串字面值。
英文:
> What is the the class name with or in a model field to have key ?
You can not a that is . , you can not use:
# wrong
print(a)
a = 42
at that time, 42 is not . The same holds for , as you found out. You can not refer to a class that is not yet. This is a of as “first-class “: a class is just an . It does not some steps.
This thus means that you can not refer to a model that has to be in the rest of the code. are where two would each other, and thus in an .
In order to fix this, to the model as a when you refer to it. you this as ‘.’ with the name of the app where the model is . If you refer to a model in the same app as the one you are , you can omit the .
will thus, after all the are , try to the and the in the model with a to the real model class.
> Which I use, the class name with or in a model field to have key ?
Both will , after that the are , in the same and the same hold, so there is no .
If you refer to that will be later in the file, or in that would in , then we don’t have much , then the is the way to “knot the tie”.
An of using the and not the is that most IDEs the and if you to a class, it the uses. IDEs can also see if you refer to an not in scope. with the also “” the and thus can do , but that is only they such logic in the IDE.
: Using an is thus than a if that is . If you use later in the file, or you would , you have to use a .
展会先锋:
商会协会:具有影响力的企业名录,助力你业绩高飞。
名家讲堂:具有最全老师阵容,帮您事业家庭腾飞。
海外ID售卖:tgnnn.cn tgmmm.com
全国展会,全国商会,全国协会,全国企业名录,全国就业会刊持续更新中,每天每周每月都会更新,感谢持续关注,
展会先锋;24小时客服VQ:371240006(展会名录全拼)
持续更新中。可以充值会员下载您需要的任何资源,有问题客服24小时在线