我是Django的新手。我按照this教程创建了一个带有MySQL数据库的Web界面。以下是我的型号代码:
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)
当我第一次编写代码时,我不小心写了“N / A'对于变量host_strain的默认选项。当我尝试迁移更改时,出现此错误:ValueError: invalid literal for int() with base 10: 'N/A'
当我将默认选项更改为“无”时,错误仍然没有消失。我甚至尝试删除我的mysql数据库中的表,但我仍然得到相同的错误。谁知道为什么?
答案 0 :(得分:2)
问题可能是django尝试使用'N/A'
值应用最新(错误)迁移并且它会中断。我建议你做的是查看你的应用程序的migrations
文件夹,并删除最新的(由于erorr而未应用)迁移或尝试自己编辑;它会
包含' N / A'作为默认值,您应该更改为其他默认值。