Answer the question
In order to leave comments, you need to log in
How to fix IndexError at /admin/auth/user/ list index out of range?
There are 2 models: customers(which contains the field user = m.ForeignKey(User, related_name="customers")) and userAdmin(class UserAdmin(NestedModelAdmin, DjangoUserAdmin)). I made a custom field that displays for each user a link to their orders. Code in app/models.py:
def orders_url(self):
return format_html(u"<a href='/admin/order/order/?q={0}'>Orders</a>", self.user.first_name)
from myproject.customer.models import Customer
def orders_url(self, user):
customer = user.customers.all()[0]
return customer.orders_url()
Error during template rendering
In template /home/vagrant/venv/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 91
list index out of range
91 {% result_list cl %}
path_to_project/customer/admin/user_admin.py in orders_url
customer = user.customers.all()[0]
▼ Local vars
Variable Value
self <myproject.customer.admin.user_admin.UserAdmin object at 0xa7e788c>
user <User: >
Answer the question
In order to leave comments, you need to log in
Solved with the following code:
def orders_url(self, user):
customers = user.customers.all()
return format_html(', '.join([customer.orders_url() for customer in customers]))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question