Django使用主键列表获取记录

2024-06-10 07:16:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的型号.py,我有两个模型:

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转换而来的well表的主键列表。你知道吗

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

使用主键获取时语法是否不同?你知道吗


Tags: nameidtrueverbosedbdatarawmodels
2条回答

首先,在关系中使用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

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

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

如果上述情况不再计算,请在调用前强制列表:

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

使用上述反向关系,您还可以:

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

事实上,您的查询是正确的,这一个肯定会起作用:

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

我认为问题出在ObjList,我怀疑这不是一个正常的列表。从你的错误报告

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

看起来ObjList具有welldrillingdetails属性,但缺少well。我的猜测是ObjList以某种方式将附加到属性名中的模型的类名转换为well属性丢失。你知道吗

相关问题 更多 >