Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

django - Displaying fields not intended to be edited in ModelAdmin

I have a custom contact form for which I create a sent_time field using auto_now_add to save the time when the user had sent the message.

I am able to list all the information on the listing view of the admin panel however when I try to enter a specific message I hit the following error:

'sent_time' cannot be specified for GeneralContact model form as it is a non-editable field

My attempt to make the fields readonly in the ModelAdmin results in the same error

class GeneralContactAdmin(ModelAdmin):
    """
    Admin model for general correspondence via
    the main contact form on the information page
    """

    model = GeneralContact
    list_display = GeneralContact.__all__
    search_fields = GeneralContact.__all__
    readonly_fields = GeneralContact.__all__
    ordering = ('-sent_time',)
    list_filter = ('sent_time', 'has_response')

Surely it is possible to be displayed only, perhaps I've done something incorrectly in my models?

Here is the base model I use for the contact model

class ContactFormBase(models.Model):

    __all__ = (
        'sent_time', 'sender_name', 'sender_email',
        'sender_message', 'has_response', 'responded_on'
    )

    sent_time = models.DateTimeField(auto_now_add=True)
    sender_name = models.CharField()
    sender_email = models.EmailField()
    sender_message = models.TextField()
    has_response = models.BooleanField(
        default=False,
        help_text='Select whether this message has been replied to by an admin.',
    )
    responded_on = models.DateTimeField(blank=True, null=True)

    panels = [
        FieldRowPanel([
            FieldPanel('sender_name'),
            FieldPanel('sender_email'),
        ]),
        FieldPanel('sent_time'),
        FieldPanel('sender_message'),
        FieldRowPanel([
            FieldPanel('has_response'),
            FieldPanel('responded_on'),
        ])
    ]

    class Meta:
        abstract = True
        ordering = ['-sent_time',]

The actual class being used is rather plain, perhaps something needs to be done here to allow display of readonly fields?

class GeneralContact(ContactFormBase, models.Model):

    panels = ContactFormBase.panels

    class Meta:
        verbose_name = 'General Contact Entry'
        verbose_name_plural = 'General Contact Entries'

In the list view all the information is able to be displayed. In the editing view, ideally there would be all of the information about the message and sender as readonly fields and an option for the admin to change the has_response value based on whether someone has responded or not.

In what way could I achieve this?


update

After seeing this Q&A I have changed the auto_now_add to use django.utils.timezone.now as the default on the sent_time attribute and life seems better, the error from the start of the question is gone and the edit view loads up entirely. However, now all the fields are editable which is not desirable.

Looking into the ModelAdmin class provided by Wagtail it appears that readonly_fields isn't available and perhaps only a feature of the django admin class of the same name. So I'm not sure what to do here. Wagtails HelpPanel type of output is what I'm looking for, and I had an idea to use that to display the data but I'm not sure what that looks like or even how it'd be done as I'm just learning django and wagtail.


update 2

Attempted to use HelpPanel instead of FieldPanel in order to try display the values but seems as if the HelpPanel doesn't retrieve the value of the attributes. Checking through these docs I see the mention of things like djangos readonly_field is not included which confirms why one of my former attempts didn't work but I did find mention of inspect_view_enabled which displays the values in a read only fashion and after trying it out it looks very much how I was trying to get it, alas, nothing there is editable which makes sense but I am getting closer.

I am wondering if a good solution would be to override the view or template used for GeneralContactAdmin but unsure if that's the right way to go about it just to output some text for one class.

A simpler solution is to keep the inspect view and only add the has_response to the edit view, but two views, one of which would only be a checkbox is not a nice for UX.

Surely there is a better way to solve this?

question from:https://stackoverflow.com/questions/66052263/displaying-fields-not-intended-to-be-edited-in-modeladmin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...