python - Django 使用主键列表来获取记录

标签 python mysql sql django

在我的 models.py 中,我有 2 个模型:

class Well(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True, verbose_name='DNS') 
    block = models.ForeignKey(Block, db_column='Block_ID', verbose_name='Block')
    uwi = models.CharField(db_column='UWI', max_length=20, blank=True, verbose_name='UWI') 
    welllocation = models.ForeignKey('Welllocation', db_column='WellLocation_ID', verbose_name='Location')
    # Plus a number of other columns

class Welldrillingdetails(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True, verbose_name='DNS') 
    well = models.ForeignKey(Well, db_column='Well_ID', verbose_name='Well') 
    casingdetails = models.ForeignKey(Casingdetails, db_column='CasingDetails_ID', verbose_name='Casing Name')
    holesize = models.CharField(db_column='holeSize', max_length=15, blank=True, verbose_name='Hole Size (inch)')
    # Plus a number of other columns

Welldrillingdetails使用Well表的pk作为外键。 我有一个 python 字典,其中有一个元素列表,其中包含我从 JSON 转换而来的井表主键列表。

raw_data = {"wells": [1,2], "seams": ["2"], "tables": [{"name": "Well", "fields": []}, {"name": "Stratigraphy", "fields": []}]}

当我使用以下方法获取 Welldrillingdetails 模型的对象时:

ObjList.append(Welldrillingdetails.objects.all().filter(well__in=raw_data['wells']))

工作正常。但是当我使用以下命令对 Well 表执行相同操作时:

ObjList.append(Well.objects.in_bulk(raw_data['wells']))

ObjList.append(Well.objects.all().filter(id__in=raw_data['wells']))

ObjList.append(Well.objects.all().filter(pk__in=raw_data['wells']))

它不工作并给出错误:

FieldError at /cbm/ajaxp/display_data/
Cannot resolve keyword 'well' into field. Choices are: ai, artificiallifts, block, category, coalseamdisp, coalseamseval, coredata, desorbedgascomp, dewateringdetails, drillcompletedate, drilleddepth, electrologs, gc, gl, hermtestingdate, hydrofracdata, id, kb, latitude, loggerdepth, longitude, maceral, minifractestresults, normalisedgc, objective, observations, pmrockprop, presentstatus, profile, projecteddepth, proximate, ptobjectinterval, releaseorderdate, releaseorderno, reserviorwelltestdata, rigname, rigreleasedate, spuddate, stratigraphy, toc, toposheet, triaxialstrength, ultimate, uwi, wcrfile, welldrillingdetails, welllocation, welltype

使用主键获取时语法是否不同?

最佳答案

首先在关系中使用lated_name,它可以帮助您更好地确定和识别反向关系。

class Well(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True, verbose_name='DNS') 
    block = models.ForeignKey(Block, db_column='Block_ID', verbose_name='Block', related_name="well")
    uwi = models.CharField(db_column='UWI', max_length=20, blank=True, verbose_name='UWI') 
    welllocation = models.ForeignKey('Welllocation', db_column='WellLocation_ID', verbose_name='Location', related_name="well")
    # Plus a number of other columns

class Welldrillingdetails(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True, verbose_name='DNS') 
    well = models.ForeignKey(Well, db_column='Well_ID', verbose_name='Well', related_name='drillingdetails') 
    casingdetails = models.ForeignKey(Casingdetails, db_column='CasingDetails_ID', verbose_name='Casing Name', related_name="drillingdetails")
    holesize = models.CharField(db_column='holeSize', max_length=15, blank=True, verbose_name='Hole Size (inch)')
    # Plus a number of other columns

批量不起作用,因为您的列表实际上并未通过:

ObjList.append(Well.objects.in_bulk(list(raw_data['wells'])))

如果上面的内容没有再次计算,请在调用之前强制使用列表:

well_ids = list(raw_data["wells"])
ObjList.append(Well.objects.in_bulk(well_ids))

通过上面的reverse_relation,您还可以:

ObjList.append(Welldrillingdetails.objects.select_related('well').filter(well__in=raw_data['wells']))

关于python - Django 使用主键列表来获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25196694/

相关文章:

python - 任何人都可以提供一种更 pythonic 的方式来生成莫里斯序列吗?

mysql - 在具有空值的 3 个表的层次结构中计数和分组

MySQL 终止休眠连接

sql - PostgreSQL:选择非 DISTINCT 的行数

sql - SELECT FOR UPDATE 子查询在负载下不遵守 LIMIT 子句

python - 如何从 OpenCV 图像而不是 numpy 数组中获取 TIFF 字节流?

python - 将数据从 Python 文件上传到 Django 数据库

javascript - 应用程序/代码安全

mysql - 在 mysql 中使用 Join 计数

SQL。我如何根据其值设置数据不可用并更新 ir(如果使用)?//餐厅系统模拟