Django - how can I make a cell in a table in the admin *changelist* interface editable only if it is null?

孤者浪人 提交于 2019-12-20 07:08:28

问题


I would like my data to be editable inline in the Django admin page. However, I only want some fields columns in each row to be editable. These columns will change for each row. Basically, I want a dropdown choice to be displayed if the value in a certain cell is null. If it is not null, then I don't want it to be editable and would like it to be readonly.

models.py:

class Size(models.Model):
    size = models.CharField(max_length=20, primary_key=True)

class Book(models.Model):
    title = models.CharField(max_length=100, primary_key=True)
    size = models.ForeignKey(Size, null=False)

class Pamphlet(models.Model):
    title = models.CharField(max_length=100, primary_key=True)
    size = models.ForeignKey(Size, null=True)
    book = models.ForeignKey(Book, null=True)

admin.py:

class PamphletAdmin(admin.ModelAdmin):
    model = Pamphlet
    list_editable = ('size','book')
    list_display = ('title', 'size', 'book',)

    def get_changelist_form(self, request, **kwargs):
        return PamphletChangeListForm

forms.py:

class PamphletChangeListForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(PamphletChangeListForm, self).__init__(*args, **kwargs)
        instance = kwargs.get('instance')
        if instance:
            self.fields['book'].queryset = Book.objects.filter(
                size=instance.size
            )
            if instance.size is not None:
                self.fields['size'].widget.attrs['readonly'] = 'readonly'

This setup is not working for me. The size shows as editable in the changelist form even when it is not null. Also - what have I failed to understand?


回答1:


If your field uses an input element, such as a TextField, add the readonly attribute to the field's widget's attrs dict in the changelist form's __init__ method. Something like this:

class PamphletChangeListForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(PamphletChangeListForm, self).__init__(*args, **kwargs)
        instance = kwargs.get('instance')
        if instance:
            self.fields['book'].queryset = Book.objects.filter(
                size=instance.size
            )
            if instance.size is not None:
                self.fields['size'].widget.attrs['readonly'] = 'readonly'

That won't protect you against a malicious user faking post data - for that you'd need to customize your admin further. But if you have malicious users on your admin you have bigger problems.

If your field uses a select element, you have to change it more - select attributes don't have readonly as a supported attribute. Instead, you'll want a hidden input with the unchanging value, and a text representation so the user can see what the setting is. Django does not include such a widget, but you can define your own:

class LabeledHiddenInput(forms.widgets.HiddenInput):
    def render(self, name, value, attrs=None):
        base_output = super(LabeledHiddenInput, self).render(name, value, attrs)
        if value:
            return base_output + unicode(value)
        else:
            return base_output

You might need more careful escaping or even some HTML formatting, this is just a quick example. Check the source code for the built in widgets if you need more examples.

Then you can use that widget instead of the default select:

        if instance.size is not None:
            self.fields['size'].widget = LabeledHiddenInput()


来源:https://stackoverflow.com/questions/21974827/django-how-can-i-make-a-cell-in-a-table-in-the-admin-changelist-interface-ed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!