Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 800
Hossam Salah Avatar asked Nov 25 '25 19:11

Hossam Salah


1 Answers

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

category = models.ForeignKey(Category, null=True)
like image 113
falsetru Avatar answered Nov 27 '25 07:11

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!