Friday, November 29, 2019

Re: Update _id field before .save() for ForeignKey

I have an idea.
  1. `bulk_create` all companies.
  2. `Filter`(SELECT) all `Company` and make it dictionary for retrieving company pk fast.
    1. company_dict = {
        'Name': 'pk',
  3. Create a list of `CompanyValue` with company name and company_dict.
  4. `bulk_create` the list.

I'm not sure if it will work because I haven't implemented it, but I hope it helps. 

2019년 11월 29일 금요일 오후 9시 42분 0초 UTC+9, Maxim Bulatov 님의 말:

I have lot of lists like follow:
(Name1): (V1, V2, V3, V4, ..)

And I use many threads to create many db objects:
company = Company(Name)
for x in vector:
    v = CompanyValue(company=company, value=x)

class CompanyValue(models.Model):
    company = models.ForeignKey('Company', on_delete=models.CASCADE)
    value = FloatField

This pool of threads return all lists, what I combine, filter and want put to database in one bulk_create call.
Suddenly, I found, is not ready to be linked in CompanyValue, follow code does not works:
CompanyValue.objects.bulk_create(values)      # value.company_id is null here

No way to save objects in my threads, because I need to filter objects and can do it only when all of them are collected. Also, it has performance issues 100k+ of requests is slowly enough. I have two ways to resolve:
1. I can make intermediate class or tuple and create CompanyValue objects only when companies is saved to db. Hard to support, hard to filter, not so clean.
2. I can use dirty hack to update _id field:
    for v in values: =

Can you advice more ways for me? It seems, I don't know something from django features to make it elegant.
I see 11 years old issue here ( and it marked as intended behaviour.

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment