Do not use this solution.

The Svg_AdminTextareaWidget code below deletes data on saving.

I think the reason might be that on saving it tries to save the image instead of the image source code.

I'm now digging deeper and will report back.

Mike

On 7/06/2021 6:00 pm, Mike Dewhirst wrote:
Thanks Derek - it is now working as follows ...


 class Chemical(models.Model):
    ...
    ddstructure = models.TextField(null=True, blank=True, verbose_name="2D structure")
    ...


class Svg_AdminTextareaWidget(AdminTextareaWidget):
    def render(self, name, value, attrs=None, renderer=None):
        if value:
            return force_text(self.format_value(value))


class ChemicalAdmin(admin.ModelAdmin):
    ...
    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if db_field.name == 'ddstructure':
            kwargs['widget'] = Svg_AdminTextareaWidget
        return super().formfield_for_dbfield(db_field, request, **kwargs)
    ...

Cheers

Mike


On 7/06/2021 11:50 am, Mike Dewhirst wrote:
On 6/06/2021 6:14 pm, Derek wrote:
RE - "I've looked at the AdminTextareaWidget which is probably based on the TextInput built-in widget but see no way to influence what it does. " and "How do I get it to display inline in the proper place?"

There seems to be similar question on SO:

https://stackoverflow.com/questions/6239966/add-custom-html-between-two-model-fields-in-django-admins-change-form <https://stackoverflow.com/questions/6239966/add-custom-html-between-two-model-fields-in-django-admins-change-form>

The answer with AdminFooWidget() and FooAdminForm() seems closest to your use case.

Thanks for that. Unfortunately the answer at that link is ten years old and I think Django Admin must have moved on since then. I'm still trying to debug an inexplicable KeyError but I think I understand the approach.

I'll keep trying

Cheers

mike



On Sun, 6 Jun 2021 at 09:56, Mike Dewhirst <mi...@dewhirst.com.au <mailto:mi...@dewhirst.com.au>> wrote:

    On 4/06/2021 5:15 pm, Derek wrote:
    Hi Mike

    The SVG is not going to display as SVG in a *form* - for that
    you'd need a widget, or perhaps just let users edit the text.

    OK - I'm outta my depth here. I've looked at the
    AdminTextareaWidget which is probably based on the TextInput
    built-in widget but see no way to influence what it does. In
    theory I'd create a new widget inheriting from that and use
    mark_safe() somehow.

    Then I would need to tell the admin form to use it for my
    ddstructure field.

    BUT see my template effort below


    Did you try displaying in the normal admin lists? This was my
    understanding of where you wanted to see it and the code
    snippets I posted were for that purpose.

    If you want to see it as "read only" in the form, but shown as
    SVG, perhaps you need a custom model template
    
(https://docs.djangoproject.com/en/dev/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model
    
<https://docs.djangoproject.com/en/dev/ref/contrib/admin/#templates-which-may-be-overridden-per-app-or-model>)

    Again, my feet don't touch bottom. I tried adding some code to
    change_form.html like this ...

    {% extends "admin/change_form.html" %}

    {% block content %}{{ block.super }}
    {% if original.ddstructure %}
        {{ original.ddstructure | safe }}
    {% else %}
        <p>No 2D structure image </p>
    {% endif %}

    {% endblock %}

    This kinda worked. It comes after block.super so it displays the
    svg image correctly but at the bottom of the page. AND the raw
    (or safe) code also displays in the location where I really want
    the image.

    So close but ...

    How do I get it to display inline in the proper place?

    Many thanks for hanging in there

    Cheers

    Mike



    Derek


    On Thursday, 3 June 2021 at 08:22:57 UTC+2 Mike Dewhirst wrote:

        On 3/06/2021 4:00 pm, Derek wrote:
        > >
        > > E.g.
        > >
        > > class MyModel():
        > >     svg_text = CharField()
        >
        > in my case it is ...
        >
        > class Chemical(models.Model):
        >     ...
        >     ddstructure = models.TextField(
        >         null=True,
        >         blank=True,
        >         verbose_name="2D structure",
        >     )
        >

        The above is unchanged.

        # admin
        class ChemicalAdmin(admin.ModelAdmin):

            def _ddstructure(self, obj):
                return mark_safe(obj.ddstructure)
            ...
            fieldsets = (
                (
                    None,
                    {
                        "fields": (
                            "ddstructure",        # displays svg code
                            "_ddstructure",
                            ...
                    }
            ),

        FYI it works perfectly as a column in the list display -
        but I don't
        need it there, rather in the page for the chemical.

        This is what happens if it is included in the fieldsets
        "fields" roster.


        FieldError at /admin/chemical/chemical/17/change/

        Unknown field(s) (_ddstructure) specified for Chemical.
        Check fields/fieldsets/exclude attributes of class
        ChemicalAdmin.

        Request Method: GET
        Request URL:
        http://localhost:8088/admin/chemical/chemical/17/change/
        <http://localhost:8088/admin/chemical/chemical/17/change/>
        Django Version: 3.2.3
        Exception Type: FieldError
        Exception Value:

        Unknown field(s) (_ddstructure) specified for Chemical.
        Check fields/fieldsets/exclude attributes of class
        ChemicalAdmin.

        Exception Location:
        
D:\Users\mike\envs\xxai\lib\site-packages\django\contrib\admin\options.py,

        line 712, in get_form
        Python Executable: D:\Users\mike\envs\xxai\Scripts\python.exe
        Python Version: 3.8.3


        Cheers

        Mike


        > >
        > >     def _the_svg(self):
        > >         return """<svg width="100"
        height="100">%s</svg>""" %
        > > self.svg_text
        > >         _the_svg.allow_tags = True
        >


-- Signed email is an absolute defence against phishing. This
        email has
        been signed with my private key. If you import my public
        key you can
        automatically decrypt my signature and be sure it came from
        me. Just
        ask and I'll send it to you. Your email software can handle
        signing.

-- You received this message because you are subscribed to the
    Google Groups "Django users" group.
    To unsubscribe from this group and stop receiving emails from
    it, send an email to django-users+unsubscr...@googlegroups.com
    <mailto:django-users+unsubscr...@googlegroups.com>.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/django-users/4862b67b-e5e9-40a3-b3aa-1666f4739703n%40googlegroups.com
    
<https://groups.google.com/d/msgid/django-users/4862b67b-e5e9-40a3-b3aa-1666f4739703n%40googlegroups.com?utm_medium=email&utm_source=footer>.


-- Signed email is an absolute defence against phishing. This email has
    been signed with my private key. If you import my public key you can
    automatically decrypt my signature and be sure it came from me. Just
    ask and I'll send it to you. Your email software can handle signing.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/775b0f5c-48a4-112d-b546-f08509eba03b%40dewhirst.com.au.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to