A
A
alex_deerk2016-08-25 19:06:53
Django
alex_deerk, 2016-08-25 19:06:53

Django Admin how to display links?

I decided not to make my own admin panel, because. everything you need is in Django's.
There is a form that performs a function like Feedback. When the form is saved, some information and a link to the page on which the form was called enters the database. Question: How do I display this link in the Django admin so that they are clickable and enter to that page, and not to edit the post?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-08-25
@alex_deerk

Any of the ModelAdmin fields can be a function to form whatever it pleases. Snippet from my code:

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ('__str__', 'customer_link')

    def customer_link(self, obj):
        if obj.customer:
            return u'<a href="{0}">{1}</a>'.format(reverse('admin:auth_user_change', args=(obj.customer.pk,)), obj.customer)
        else:
            return obj.customer_fio
    customer_link.allow_tags = True
    customer_link.admin_order_field = 'customer'
    customer_link.short_description = Order._meta.get_field('customer').verbose_name.title()

And for greater clarity, a piece of the Order model:
@python_2_unicode_compatible
class Order(models.Model):
    customer = models.ForeignKey(User, verbose_name=u'Заказчик', null=True, blank=True, related_name='orders')
    customer_fio = models.CharField(u'ФИО заказчика', max_length=150)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question