简体   繁体   English

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

[英]Django using list of primary keys to fetch records

In my models.py, I have 2 models : 在我的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

The table Welldrillingdetails uses pk of Well table as foreign key. Welldrillingdetails表使用Well表的pk作为外键。 I have a python dictionary that has an element list having list of primary keys of well table which I have converted from JSON. 我有一个python字典,其中有一个元素列表,其中包含从JSON转换的井表的主键列表。

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

When I am fetching the objects of Welldrillingdetails model using : 当我使用以下方法获取Welldrillingdetails模型的对象时:

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

It is working fine. 一切正常。 But when I am doing the same for the Well table using : 但是当我使用以下方式对Well表执行相同操作时:

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

or 要么

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

or 要么

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

it is not working and giving the error: 它不起作用并给出错误:

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

Is the syntax different while fetching using primary key? 使用主键获取时语法是否不同?

First of all use related_name in relationships it helps you better determine and recognize reverse relations. 首先,在关系中使用related_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

Bulk does not work because your list is not actually passed: 批量无效,因为您的列表实际上没有传递:

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

In case the above does not evaluate again, force the list before the call: 如果以上内容无法再次评估,请在通话前强行列出该列表:

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

With the reverse_relation above you can also: 通过上面的reverse_relation,您还可以:

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

Actually, your queries are correct, this one will work for sure: 实际上,您的查询是正确的,这一查询可以肯定地起作用:

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

I think a problem is in ObjList , I suspect it's not a normal list. 我认为ObjList存在问题,我怀疑它不是正常列表。 From your bug report 来自您的错误报告

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

it looks like ObjList has welldrillingdetails attribute but is missing well . 看来ObjList具有welldrillingdetails属性,但缺少得well My guess is that ObjList somehow translates class name of model you are appending into attribute name and well attribute is missing. 我的猜测是ObjList以某种方式将您要追加的模型的类名称转换为属性名称, well缺少属性。

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

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