在我的models.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
使用pk
表Well
作为外键。
我有一个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']))
工作正常。但是当我使用:{/ p>为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
使用主键获取时语法是否不同?
答案 0 :(得分:0)
首先在关系中使用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))
使用上面的reverse_relation,您还可以:
ObjList.append(Welldrillingdetails.objects.select_related('well').filter(well__in=raw_data['wells']))
答案 1 :(得分:0)
实际上,您的查询是正确的,这个问题肯定会有效:
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
属性。