Hossam Salah Hossam Salah - 3 months ago 29
Python Question

models Django invalid literal for int() with base 10: 'None'

I am new to Django, I am tring to make a Blog here is my models

from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):
name = models.OneToOneField(User)

def __unicode__(self):
return self.user.user_name


class Category(models.Model):
category = models.CharField(max_length=150)

def __str__(self):
return self.category


class Blog(models.Model):
title = models.CharField(max_length=150, default="No title")
body = models.TextField(default="None")
creation_date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(UserProfile, default=1)
category = models.ForeignKey(Category, default="None")

def __str__(self):
return self.title


class Comment(models.Model):
comment = models.TextField(default="")
blog = models.ForeignKey(Blog)


but when i run the command:


python manage.py migrate


i get the


ValueError: invalid literal for int() with base 10: 'None'


I tried to delete the old migration file and migrate again but I got the same error, what should I do?

Answer

Instead of default="None", specify null=True to allow null for foreign key:

category = models.ForeignKey(Category, null=True)