第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 django 中批量創(chuàng)建具有相關(guān)對(duì)象的對(duì)象的有效方法是什么?

在 django 中批量創(chuàng)建具有相關(guān)對(duì)象的對(duì)象的有效方法是什么?

哈士奇WWW 2023-10-31 14:07:44
我有以下型號(hào):class LocationPoint(models.Model):    latitude = models.DecimalField(max_digits=16, decimal_places=12)    longitude = models.DecimalField(max_digits=16, decimal_places=12)    class Meta:        unique_together = (            ('latitude', 'longitude',),        )class GeoLogEntry(models.Model):    device = models.ForeignKey(Device, on_delete=models.PROTECT)    location_point = models.ForeignKey(LocationPoint, on_delete=models.PROTECT)    recorded_at = models.DateTimeField(db_index=True)    created_at = models.DateTimeField(auto_now_add=True, db_index=True)我有很多傳入記錄要?jiǎng)?chuàng)建(可能一次有數(shù)千個(gè))。目前我這樣創(chuàng)建它們:# Simplified map function contents (removed mapping from dict as it's unrelated to the question topicpoints_models = map(lambda point: LocationPoint(latitude=latitude, longitude=longitude), points)LocationPoint.objects.bulk_create(     points_models,     ignore_conflicts=True)# Simplified map function contents (removed mapping from dict as it's unrelated to the question topicgeo_log_entries = map(            lambda log_entry: GeoLogEntry(device=device, location_point=LocationPoint.objects.get(latitude=latitude, longitude=longitude), recorded_at=log_entry.recorded_at),            log_entries        )GeoLogEntry.objects.bulk_create(geo_log_entries, ignore_conflicts=True)但我認(rèn)為它不是很有效,因?yàn)樗\(yùn)行記錄N SELECT查詢N。有更好的方法嗎?我使用 Python 3.9、Django 3.1.2 和 PostgreSQL 12.4。
查看完整描述

2 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

主要問(wèn)題是獲取要批量鏈接的對(duì)象。一旦我們存儲(chǔ)了所有這些對(duì)象,我們就可以批量獲取對(duì)象:


from django.db.models import Q


points_models = [

    LocationPoint(latitude=point.latitude, longitude=point.longitude)

    for point in points

]


LocationPoint.objects.bulk_create(

     points_models,

     ignore_conflicts=True

)


qfilter = Q(

    *[

          Q(('latitude', point.latitude), ('longitude', point.longitude))

          for point in log_entries

    ],

    _connector=Q.OR

)



data = {

    (lp.longitude, lp.latitude): lp.pk

    for lp in LocationPoint.objects.filter(qfilter)

}


geo_log_entries = [

    GeoLogEntry(

        device=entry.device,

        location_point_id=data[entry.longitude, entry.latitude],

        recorded_at=entry.recorded_at

    )

    for entry in log_entries

]


GeoLogEntry.objects.bulk_create(geo_log_entries, ignore_conflicts=True)

因此,我們批量獲取需要鏈接到的所有對(duì)象(因此使用一個(gè)查詢),創(chuàng)建一個(gè)映射主鍵上的經(jīng)度和緯度的字典,然后設(shè)置為該點(diǎn)location_point_id。


然而,重要的是使用小數(shù),或者至少是一種匹配的類型。浮點(diǎn)數(shù)很棘手,因?yàn)樗鼈兒苋菀桩a(chǎn)生舍入誤差(因此經(jīng)度和緯度通常存儲(chǔ)為“定點(diǎn)”數(shù)字,例如整數(shù)大 1'000 倍或大 1'000'000 倍)。否則,您應(yīng)該使用將其與通過(guò)查詢生成的數(shù)據(jù)相匹配的算法。


查看完整回答
反對(duì) 回復(fù) 2023-10-31
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

bulk_create(...)將以列表形式返回您創(chuàng)建的對(duì)象。您可以在 Python 端過(guò)濾這些對(duì)象,而不是查詢數(shù)據(jù)庫(kù),因?yàn)樗鼈円呀?jīng)被獲取。


location_points = LocationPoint.objects.bulk_create(

     points_models,

     ignore_conflicts=True

)


geo_log_entries = map(

    lambda log_entry: GeoLogEntry(

        device=device, 

        location_point=get_location_point(log_entry, location_points),      

        recorded_at=log_entry.recorded_at

    ),

    log_entries

)


GeoLogEntry.objects.bulk_create(geo_log_entries, ignore_conflicts=True)

您所需要做的就是實(shí)現(xiàn)get_location_point滿足您的需求


查看完整回答
反對(duì) 回復(fù) 2023-10-31
  • 2 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)