[Answer]-Django : 1048, "Column 'category_id' cannot be null"

1👍

This looks like a problem with your ‘ItemSubcategory’ model in that when you create an instance of it… you don’t supply a ‘category’ for it, only a ‘name’. When ‘category’ is required. You will need to either supply a ‘category’ or make it so that this field can accept null values.

Another thing worth mentioning is that your code isn’t very “Pythonic” – it took me a minute to work out what exactly your code was doing, when it could be as simple as:

# ‘sub_category’ is the model instance
# ‘created’ is True if the object was created and False if it was retrieved
sub_category, created = ItemSubcategory.objects.get_or_create(name = page_item['slottype'])

ni.sub_category = sub_category
ni.save()
👤Matt

Leave a comment