添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have defined two models where each one references the other, like so:

class User(models.Model):

# ...

loves = models.ManyToManyField(Article, related_name='loved_by')

class Article(models.Model):

# ...

author = models.ForeignKey(User)

You see, the problem is both classes references each other. No matter in what order these two classes are implemented, python always raises NameError exception, complaining either one class is not defined.

You can find the solution in the docs:

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:

class Car(models.Model):

manufacturer = models.ForeignKey('Manufacturer')

# ...

class Manufacturer(models.Model):

# ...

我有以下 两个 :class Product(db.Model):__tablename__ = 'product'ProductID = db.Column(db.Integer, primary_key=True)StartOperatorID = db.Column(db.Integer, db.ForeignKey('user.UserID'), nullable=False)StartOp... 显示满足查询条件的子集: class ExampleView(ListView): queryset = Example.objects.filter(fieldname='something') 第三个:get_ 需求:近一段时间基于 django 框架,开发各业务层监控代码,每个业务的监控逻辑不同,因此需要开发监控子模块,动态的导入 调用 。 项目名称:demo_ django App:common_base、monitor_master 监控子模块:demo_ django /common_base/modules/classname.py # -*- coding: utf-8 -*- class clas... 第一个app下的model from django .contrib.auth.models import AbstractUser from django .db import models # Create your models here. class UserInfo(AbstractUser): telephone=models.CharField(max_length=11,verbose_name='手机号',null=True,help_text='11位电话号码')