3

I have this as my django display

The code for doing this is:

class PurchaseOrder(models.Model):
    product = models.ManyToManyField('Product', null =True)

    def get_products(self):
        return "\n".join([p.products for p in self.product.all()])

class Product(models.Model):
    products = models.CharField(max_length=256, null =True)

    def __unicode__(self):
         return self.products

views.py:

class PurchaseOrderAdmin(admin.ModelAdmin):
   fields = ['product']
   list_display = ('get_products')

So I've attempted to do this:

from django.utils.html import format_html

 def get_products(self):
    return format_html(" <p> </p>").join([p.products for p in self.product.all()])

however, it's still not returning it in HTML. Or rather, the way I want it in the image

1 Answer 1

2

You need to set the field to allow tags for the HTML to show up in the admin list display:

class PurchaseOrder(models.Model):
    product = models.ManyToManyField('Product', null =True)

    def get_products(self):
        return "<br />".join([p.products for p in self.product.all()])
    get_products.allow_tags = True

See the docs for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there an allow tag while you're in models? I actually can't get this to compile while in admin. Any idea how to get it for models?
My sample is on your model class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.