简体   繁体   English

如何枚举新列?

[英]How to enumerate new column?

In my Django admin I have a TabularInLine class. 在我的Django管理员中,我有一个TabularInLine类。 In the picture, each element of this class is numbered with it's own ID . 在图片中,此类的每个元素都用自己的ID This TabularInLine class has a foreign key to an other class (class Part ). TabularInLine类具有另一个类( Part类)的外键。 How I can to numerate that for each FK with the numbering ( my_id ) started from 1 as in the picture below? 如何为每个FK编号,编号( my_id )从1开始,如下图所示?

在此处输入图片说明

you can define your own insert operation which does those steps: 您可以定义自己的insert操作,执行以下步骤:

  • query database with your new entry's foreign key 使用新条目的外键查询数据库
  • get count of results 得到结果计数
  • assign my_id=count+1 分配my_id = count + 1

let's say you already have that table in your database. 假设您的数据库中已经有该表。 you want to add a new entry to database, which has FK="three" . 您想要向数据库添加一个新条目,该条目具有FK="three"

  • query DB for objects( FK="three" ). 查询数据库中的对象( FK="three" )。
  • count of results is 2. (the last two rows) 结果计数为2。(最后两行)
  • assign my_id=3 分配my_id = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM