0

我有两个通过 ManyToManyField 关系相关的 Django 模型。一切正常,除了内联添加下拉列表列出丑陋的自动创建的对象名称,而不是让我格式化它。我该如何指定?

楷模:

class Job(models.Model):
    type = models.CharField(max_length=32, choices=JobChoices)
    guid = models.CharField(max_length=32)
    title = models.CharField(max_length=256)
    started_time = models.DateTimeField()
    ended_time = models.DateTimeField(blank=True, null=True)
    enabled = models.BooleanField(default=False)
    running = models.BooleanField(default=False)
    working_job_status = models.CharField(max_length=32, choices=StatusCoices)
    working_job_length = models.IntegerField(blank=True, null=True)
    working_job_progress = models.IntegerField(blank=True, null=True)
    working_job_eta_sec = models.IntegerField(blank=True, null=True)

RepeatUnit = (
    ('s', 'Second'),
    ('m', 'Minute'),
    ('h', 'Hour'),
    ('d', 'Day'),
    ('W', 'Week'),
    ('M', 'Month'),
    ('Y', 'Year'),
)

class Schedule(models.Model):
    title = models.CharField(max_length=128)
    job = models.ManyToManyField(Job, blank=True, null=True)
    start_time = models.DateTimeField(null=False)
    end_time = models.DateTimeField(blank=True, null=True)
    repeat_unit = models.CharField(blank=True, null=True, max_length=1, choices=RepeatUnit)
    repeat_every = models.IntegerField(blank=True, null=True)
    repeat_max_count = models.IntegerField(blank=True, null=True)

    def __unicode__(self):
        return f'{self.title}'

行政:

class ScheduleAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'start_time', 'end_time', 'repeat_unit', 'repeat_every', 'repeat_max_count']

class ScheduleInline(admin.TabularInline):
    model = Schedule.job.through
    min_num = 0
    extra = 0
    # fields = ('title', )
    verbose_name = "Schedule"
    verbose_name_plural = "Schedules"

class JobAdmin(admin.ModelAdmin):
    list_display = ['id', 'type', 'guid', 'title', 'started_time', 'ended_time', 'enabled', 'running', 'progress']
    inlines = [ScheduleInline,]

admin.site.register(Schedule, ScheduleAdmin)
admin.site.register(Job, JobAdmin)

而且,当我点击内联下拉菜单时,我得到: 内联菜单

4

1 回答 1

1

__unicode__(self)变成__str__(self)成功了

于 2018-04-20T21:41:20.693 回答