我有几个模型都有ForeignKey到另一个模型。这允许我使用多个选项的多个项目创建“作业”。
问题是:当我没有用数据填充文本框时,我无法添加项目。但就我而言,数据是可选的......
是否有任何解决方案(或黑客)强制添加字段?
我附上我的模型示例,以及显示我的意思的管理面板的屏幕截图。
models.py:
class Job(models.Model):
name = models.CharField(max_length=128)
class FtpJob(models.Model):
host = models.CharField(max_length=128)
username = models.CharField(max_length=64)
password = models.CharField(max_length=64, null=True, blank=True)
job = models.ForeignKey(Job, on_delete=models.CASCADE)
class PopulateFileListJob(models.Model):
issue_subid = models.CharField(max_length=1024, null=True, blank=True)
job = models.ForeignKey(Job, on_delete=models.CASCADE)
class CreateZipJob(models.Model):
issue_subid = models.CharField(max_length=1024, null=True, blank=True)
job = models.ForeignKey(Job, on_delete=models.CASCADE)
admin.py:
class FtpJobInlineAdmin(admin.StackedInline):
model = FtpJob
extra = 0
class PopulateFileListJobInlineAdmin(admin.StackedInline):
model = PopulateFileListJob
extra = 0
class CreateZipJobInlineAdmin(admin.StackedInline):
model = CreateZipJob
extra = 0
class JobAdmin(admin.ModelAdmin):
inlines = [
FtpJobInlineAdmin,
PopulateFileListJobInlineAdmin,
CreateZipJobInlineAdmin,
]
admin.site.register(Job, JobAdmin)
屏幕截图显示了当字段为空时不会添加项目的示例。
答案 0 :(得分:0)
如果我有下注,请尝试添加"默认"
示例:
host = models.CharField(max_length=128, default="")