1

I'm trying to bulk create some entries using bulk_create() method of Django in the DB table. On the final step where I'm passing the created list over there getting an error TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' and due to this obviously no records are getting saved.

Below is the code through which I'm trying to save the records in the DB table.

Class method

@classmethod
def mark_all_added(cls, id: int, spec_id: int) -> None:
    spec_ids = set(
        cls.objects.filter(spec_id=spec_id)
        .values_list("id")
    )

    through_objects = [
        cls.added_by.through(id=id, spec_id=sid)
        for sid in spec_ids
    ]

    cls.added_by.through.objects.bulk_create(through_objects) # getting error on this line

Could someone please help in highlighting my mistake or tell me how to resolve this issue while saving bulk records. I know I have a done a silly mistake but am unable to trace it. Any kind of would be much appreciated. Thanks in advance.

5
  • Can you please provide the full traceback? Commented Aug 2, 2021 at 10:49
  • It is a little difficult for me to add the full traceback as there is a lot of information that is visible related to the project. Even the above code is a sanitised one. Commented Aug 2, 2021 at 10:51
  • Are you sure there is no trailing comma at the line cls.added_by.through(id=id, spec_id=sid)? so cls.added_by.through(id=id, spec_id=sid),. Commented Aug 2, 2021 at 10:52
  • Yes I'm 100% sure there is no comma Commented Aug 2, 2021 at 10:53
  • nvm, I found it, you should make a "flat" values_list, it thus turns out you provided enough information. Commented Aug 2, 2021 at 10:56

1 Answer 1

1

You need to make a flat list of 'id's, not a QuerySet of singleton tuples, by specifying flat=True in the .values_list(…) method [Django-doc], so:

@classmethod
def mark_all_added(cls, id: int, spec_id: int) -> None:
    spec_ids = set(
        cls.objects.filter(spec_id=spec_id)
        .values_list('id', flat=True)  # ← use flat=True
    )
    # …
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, must say you have eagle eyes. Brilliant and awesome you are it perfectly worked for me that was the only mistake. Bingo thank you so much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.