Pen Gerald
Pen Gerald

Reputation: 31

invalid literal for int() with base 10: 'N/A' Django

I'm new to Django. I followed this tutorial to create a web interface with a MySQL database. The following is my model code:

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

# Create your models here.


class Strain(models.Model):
    """;
    Model representing strains.
    """
    strain_name = models.CharField(max_length=100, blank=False, null=True, default=None)
    data_of_generation = models.DateField(null=True, blank=True, default= None)

    submitter = models.ForeignKey(User,null=True, blank = False, on_delete= models.CASCADE, default= None)

    class Meta:
        ordering = ["strain_name"]

    def get_absolute_url(self):
        """
        Returns the url to access a particular author instance.
        """
        return reverse('strain-detail', args=[str(self.id)])

    def __str__(self):
        """
        String for representing the Model object.
        """
        return '{0}'.format(self.strain_name)


class Plasmid(models.Model):
    """;
    Model representing an Plasmid Database.
    """
    plasmid_name = models.CharField(max_length=35, blank=False)
    data_of_generation = models.DateField(null=True, blank=True, default= None)
    restriction_sites = models.CharField(max_length=100, blank=False, null=True)

    host_strain = models.ForeignKey(Strain, null=True, blank = True, on_delete= models.CASCADE, default= None)


    class Meta:
        ordering = ["plasmid_name"]

    def get_absolute_url(self):
        """
        Returns the url to access a particular author instance.
        """
        return reverse('plasmid-detail', args=[str(self.id)])

    def __str__(self):
        """
        String for representing the Model object.
        """
        return '{0}'.format(self.plasmid_name)

When I first wrote the code, I accidentally wrote 'N/A' for the default option of variable host_strain. When I tried to migrate the changes, I got this error: ValueError: invalid literal for int() with base 10: 'N/A'

When I changed the default option to None, the error still didn't go away. I even tried deleting the tables in my mysql database, but I still get the same error. Anyone know why?

Upvotes: 1

Views: 689

Answers (1)

Serafeim
Serafeim

Reputation: 15104

The problem is probably that django tries to apply the latest (erroneous) migration with the 'N/A' value and it breaks. What I recommend you to do is take a look at the migrations folder of your application and either delete the latest (non-applied due to the erorr) migration or try editing it yourself; it will contain the 'N/A' as default value which you should change to a different default.

Upvotes: 2

Related Questions