在我的 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/