3

I am trying to bulk insert data into the database but upon appending these instances i find they are none

model

 class Seekerskillset(models.Model):
        skill_set = models.ForeignKey(Skillset, on_delete=models.CASCADE)
        seeker = models.ForeignKey(SeekerProfile, on_delete=models.CASCADE)
        skill_level = models.CharField(max_length=25)
    
        class Meta:
            verbose_name = 'Seeker skill set'

my view

 for skill_nme, skill_lvl in zip(skill_name, skill_level):
        skill_set = Skillset.objects.get(skill_name=skill_nme)
        seeker_skll.append(Seekerskillset(
            skill_set=skill_set, skill_level=skill_lvl, seeker=user))
        print(seeker_skll)
  

    seeker_bulk = Seekerskillset.objects.bulk_create(seeker_skll)
    print('after insertion',seeker_bulk)
    return redirect('/users/dashboard')

trace

<class 'seekerbuilder.models.SeekerProfile'>
[<Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>]
after insertion [<Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>, <Seekerskillset: Seekerskillset object (None)>]
[19/Jul/2021 14:27:12] "POST /users/app_det/ HTTP/1.1" 302 0
[19/Jul/2021 14:27:12] "GET /users/dashboard HTTP/1.1" 301 0
Atif Shafi
  • 954
  • 1
  • 11
  • 26

1 Answers1

0

The item between brackets (here (None)) is the primary key of the object. So if you do not override the __str__ or __repr__ method of the model, it will print this with:

<ModelName: ModelName object (pk)>

When doing a .bulk_create(…) [Django-doc] for some databases, Django is able to retrieve the primary key, for other databases that is not possible (since the database for example does not contain any logic to return values when creating an object). As is specified by the documentation:

If the model's primary key is an AutoField, the primary key attribute can only be retrieved on certain databases (currently PostgreSQL, MariaDB 10.5+, and SQLite 3.35+). On other databases, it will not be set.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    thankyou soo much! i had trouble understanding that – Atif Shafi Jul 19 '21 at 09:22
  • Does SQLite still work? I use it with SQLite and doesn't work. In Django-doc it says "If the model’s primary key is an AutoField, the primary key attribute can only be retrieved on certain databases (currently PostgreSQL and MariaDB 10.5+). On other databases, it will not be set." – nogabemist Nov 22 '21 at 08:20
  • @nogabemist: as of Django-4.0, it will work again, as is specified in the documentation: "*Support for the fetching primary key attributes on SQLite 3.35+ was added.*" – Willem Van Onsem Nov 22 '21 at 11:21