如何使用验证在管理员中创建可点击的伪只读文件字段?

发布于 2024-11-25 12:12:27 字数 1002 浏览 0 评论 0原文

我试图拥有一个可在管理员中单击但也是只读的 FileField。目前针对此问题有一个开放的票据,但我现在需要一个解决方法。我正在尝试为我的管理类编写一个验证器,但在运行它时遇到了异常。这就是我目前所拥有的:

class ModelWithAttachment(models.Model):
    attachment = FileField(upload_to=somewhere, blank=True)

class ModelWithAttachmentAdminForm(forms.ModelForm):
    class Meta:
        model = ModelWithAttachment

    def clean_attachment(self):
        attachment = self.cleaned_data['attachment']
        return self.cleaned_data['attachment']

class ModelWithAttachmentAdmin(admin.ModelAdmin):
    form = ModelWithAttachmentAdminForm

目前,我收到一个断言错误,在 attachment = self.cleaned_data['attachment'] 行中没有提供任何异常。如果我用 cleaned_data = self.cleaned_data 替换该行,我会得到相同的 AssertionError。据我了解, self.cleaned_data 应该是在验证过程的早期创建的,所以我不明白为什么它似乎不存在。

其次,我整个方案的目标是检查通过管理员提交的附件的值与当前持有的值,如果两者不同,则拒绝它(引发 ValidationError) - 实质上使附件“只读”,同时​​允许在管理中单击它。这是一个可行的目标吗?还有另一种更好/更简单的方法来实现这一点吗?

I'm trying to have a FileField that is clickable in the admin but is also readonly. There's currently an open ticket for this issue, but I need a workaround now. I'm trying to write a validator for my admin class but I'm running into an exception when I run it. This is what I currently have:

class ModelWithAttachment(models.Model):
    attachment = FileField(upload_to=somewhere, blank=True)

class ModelWithAttachmentAdminForm(forms.ModelForm):
    class Meta:
        model = ModelWithAttachment

    def clean_attachment(self):
        attachment = self.cleaned_data['attachment']
        return self.cleaned_data['attachment']

class ModelWithAttachmentAdmin(admin.ModelAdmin):
    form = ModelWithAttachmentAdminForm

Currently I get an AssertionError with no exception supplied at the line attachment = self.cleaned_data['attachment']. If I replace that line with cleaned_data = self.cleaned_data, I get the same AssertionError. As far as I understand it, self.cleaned_data is supposed to have been created earlier in the validation process, so I don't understand why it doesn't seem to exist.

Secondly, my goal with this whole scheme is to check the value of the attachment being submitted through the admin against the value it currently holds, and reject it (raise a ValidationError) if the two differ - essentially making the attachment 'readonly' while allowing it to be clicked in the admin. Is this a feasible goal? Is there another better/simpler way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-12-02 12:12:27

我想通了。我的方法是正确的,clean_attachment 被定义为:

def clean_attachment(self): 
    if 'attachment' in self.changed_data: 
        raise forms.ValidationError('no!') 

    return self.cleaned_data['attachment'] 

问题是旧的 .pyc 文件被错误地重用。一旦我删除了它,就很好了。希望能帮助别人。

I figured it out. My approach was correct, with clean_attachment being defined as:

def clean_attachment(self): 
    if 'attachment' in self.changed_data: 
        raise forms.ValidationError('no!') 

    return self.cleaned_data['attachment'] 

The problem was an old .pyc file getting reused incorrectly. Once I deleted that, it was fine. Hope that helps someone else out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文