转自:https://blog.csdn.net/lanyang123456/article/details/68962515

问题:

定义表Apple:

class Apple( models.Model):
origin_level = models.ForeignKey(AppleLevel)
new_level = models.ForeignKey(AppleLevel)

出现如下问题:

monitor.apple: Accessor for field ‘origin_level’ clashes with related field ‘AppleLevel.apple_set’. Add a related_name argument to the definition for ‘origin_level’. 
monitor.apple: Accessor for field ‘new_level’ clashes with related field ‘AppleLevel.apple_set’. Add a related_name argument to the definition for ‘new_level’.

原因:

一个数据表同时两次外键引用另一个表,出现重名问题。

解决办法:

使用related_name属性定义名称(related_name是关联对象反向引用描述符)。 
具体修改代码如下:

class Apple( models.Model):
origin_level = models.ForeignKey(AppleLevel, related_name='orgin_level_appleset')
new_level = models.ForeignKey(AppleLevel, related_name='new_level_appleset')

related_name使用之后,有什么用处呢? 
用处就是: 
通过AppleLevel可以得到引用自身的Apple对象。

例如, 
通过origin_level引用AppleLevel 中id为12的Apple的所有对象

AppleLevel.object.get(id=12).origin_level_appleset.objects.all()

通过new_level引用AppleLevel 中id为12的Apple的所有对象

AppleLevel.object.get(id=12).new_level_appleset.objects.all()

Django model 反向引用中的related_name的更多相关文章

  1. Django Model之引用一个未定义的Model

    Django Model的外键自关联

  2. Django model反向关联名称的方法(转)

    原文:https://www.jb51.net/article/152825.htm

  3. Django model总结(上)

    Django model是django框架中处于比较核心的一个部位,准备分三个博客从不同的方面分别进行阐述,本文为<上篇>,主要对[a]Model的基本流程,比如它的创建,迁移等:默认行为 ...

  4. python django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct[转]

    1.多表连接查询:当我知道这点的时候顿时觉得django太NX了.   class A(models.Model):     name = models.CharField(u'名称')   clas ...

  5. django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

    1.多表连接查询:当我知道这点的时候顿时觉得django太NX了.  class A(models.Model):    name = models.CharField(u'名称')  class B ...

  6. Django:model中的ForeignKey理解

    有两个数据模型栏目模型和文章模型ArticleColumn和ArticlePost ArticleColumn: class ArticleColumn(models.Model): # 用户与栏目是 ...

  7. django中跨app引用model

    可能是自己水平的原因,总感觉跨django中app引用有点怪怪的,所以在自己没有达到另一个级别之前就先把正确的解决 方案记一下吧. 一.django中跨app引用model,以app02中的model ...

  8. Django model中 双向关联问题,求帮助

    Django model中 双向关联问题,求帮助 - 开源中国社区 Django model中 双向关联问题,求帮助

  9. Django model 中的 class Meta 详解

    Django model 中的 class Meta 详解 通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(mode ...

随机推荐

  1. IO获取指定目录及其目录下子目录

    一.需求:获取指定目录下,指定扩展名的文件(包含子目录中的) 二.需要用到的方法 1.FilenameFilter :返回抽象路径名的定义中表示此抽象路径名的目录中的文件的数组.  filter.ac ...

  2. win7-64bit安装comtypes的问题

    Update 28/12/2014: Please download the latest comtypes 1.1.1 from https://pypi.python.org/pypi/comty ...

  3. malloc内存分配

    网上总结到的信息: (1) 静态分派:是在栈上分配,是由用户自己申请,是由操作系统自己释放的 动态分配:是由编译器分配,操作系统没有提供这样的机制,所以自己申请,必须自己删除! (2)你也要明确.栈的 ...

  4. JAVA注解引发的思考

    自从JDK5開始Java開始添加了对元数据(MetaData)的支持,也就是注解(Annotation),到JDK7时已经有四种基本注解,新添加了一种@SafeVarargs. @Override注解 ...

  5. oracle连接串的一种写法

    我在.NET项目里访问oracle,向来是规规矩矩地这样写: DATA SOURCE=PDBGZFBC;PASSWORD=test;PERSIST SECURITY INFO=True;USER ID ...

  6. 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数

    一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x)  返回大于或等于x的最小整数 SELEC ...

  7. Apache Hadoop 3.0.0 Release Notes

    http://hadoop.apache.org/docs/r3.0.0/hadoop-project-dist/hadoop-common/release/3.0.0/RELEASENOTES.3. ...

  8. Oracle常用SQL与练习

    基本 数学函数 rownum相关 分页查询 (假设每页显示10条) 不包含排序: 包含排序: 时间处理 1. to_char和to_date基本使用 eg1: eg2: 2)months_betwee ...

  9. RK3288-OTA编译失败解决办法【转】

    本文转载自:http://blog.csdn.net/wangxueming/article/details/52448630 在执行make  otapackage的时候出现该错误,是由于drmsi ...

  10. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...