0

I'm Inserting multiple images to database. I have saved the new Image name is Dictonary. But while inserting the data to table i'm getting ERROR : 'int' object is not subscriptable'

I have searched everywhere but couldn't find solution.

Here is below how Dictonary looks like

    {
    "0" = {
        "blog_id" = "". 
        "blog_image_path" = "07-09-2018__17-54-152069weather.jpg". 
        "is_deleted" = 0
    }. 
    "1" = {
        "blog_id" = "". 
        "blog_image_path" = "07-09-2018__17-54-152069user.png". 
        "is_deleted" = 0
    }. 
    "2" = {
        "blog_id" = "". 
        "blog_image_path" = "07-09-2018__17-54-152069tick.png". 
        "is_deleted" = 0
    }
}

Now while adding the DB

for image in blog_images_data:
    blog_image_data = BlogImages(blog_id=int(blog_id), blog_image_path=image['blog_image_path'], is_deleted=int(image['is_deleted']))# I GET ERROR IN THIS LINE
    blog_image_data.save()

I have even changed the datatype to INT but still error persists.

Below is the Model Details

class BlogImages(models.Model):
blog_image_id = models.AutoField(primary_key=True)
blog = models.ForeignKey(Blog, null=True, on_delete=models.CASCADE)
blog_image_path = models.TextField(null=True)
is_deleted = models.SmallIntegerField(default=0, editable=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
    db_table = 'tbl_blog_images'

THANKS IN ADVANCE

2
  • Your dictionary looks very strainge, normally the format is { key: value }, not { key = value }. Commented Sep 7, 2018 at 18:00
  • Furthermore it looks to me that image in your code is just an int (for example 42, so then image['blog_image_path'] indeed makes no sense, you iterated over the keys. Commented Sep 7, 2018 at 18:01

1 Answer 1

1

In your code the for loop iterates over the keys of the dictionary. So image is 0, 1, 2, etc.

If this is indeed a dictionary (the syntax is not really correct), it should look like:

for blog_id, image in blog_images_data.items():
    blog_image_data = BlogImages(
        blog_id=int(blog_id),
        blog_image_path=image['blog_image_path'],
        is_deleted=int(image['is_deleted'])
    )
    blog_image_data.save()

So we iterate over key-value pairs with .items(), and do the processing with image being the subdictionary.

We can perform the creation of the object and saving it to the database in a single call with:

for blog_id, image in blog_images_data.items():
    blog_image_data = BlogImages.objects.create(
        blog_id=int(blog_id),
        blog_image_path=image['blog_image_path'],
        is_deleted=int(image['is_deleted'])
    )

Note: normally models have singlar names, so BlogImage, instead of BlogImages.

Sign up to request clarification or add additional context in comments.

Comments

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.