Django TabularInline只有readonly_fields但我的FK对象仍然显示和可编辑

时间:2017-01-25 17:15:30

标签: django django-admin

我在Admin中创建了一个只读的Tabular Inline,我无法摆脱StatusObject的这个额外下拉列表。正如您所看到的,我只有3个readonly_fields,但是为TaskStatus.status显示了一个额外的字段,并且它是可编辑的。知道为什么吗?我确定我忽视了一些简单的事情。

TabularInline

class TaskStatusInline(admin.TabularInline):
    model = TaskStatus
    extra = 0

    readonly_fields = [
        'status_name',
        "updated_by",
        'timestamp'
    ]

    can_delete = False

    def has_add_permission(self, request, obj=None):
        return False

    def status_name(self, obj):
            return obj.status.name

模型

class TaskStatus(models.Model):
    class Meta:
        ordering = ['-timestamp']

    status = models.ForeignKey('status')
    task = models.ForeignKey('Task', related_name="task_status")
    timestamp = models.DateTimeField(auto_now_add=True)
    updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)


class Status(models.Model):
    class Meta:
        unique_together = ['status', 'substatus']
        verbose_name_plural = "status"
        ordering = ['code']

    name = models.CharField(max_length=50)
    status = models.CharField(max_length=25)
    substatus = models.CharField(max_length=25)
    code = models.IntegerField()

1 个答案:

答案 0 :(得分:1)

class TaskStatusInline(admin.TabularInline):
    model = TaskStatus
    extra = 0
    fields = [
        'status_name',
        'updated_by',
        'timestamp'
    ]
    readonly_fields = [
        'status_name',
        "updated_by",
        'timestamp'
    ]