如何访问访问器中的列表元素
How to access elements of list in accessor
我有两个数据模型,company
和 contact_person
。它们以 m2m 变体链接:
models.py:
class ContactPerson(models.Model):
name = models.CharField('first name', max_length=120)
@property
def contact_name(self):
return [self.name, self.id]
class Customer(models.Model):
name = models.CharField('company name', max_length=120)
contact_persons = models.ManyToManyField(ContactPerson, blank=True, null=True)
@property
def contacts(self):
persons = []
for c in self.contact_persons.all():
persons.append({"name": c.contact_name[0], "id": c.contact_name[1]})
return persons
tables.py:
class CustomerTable(django_tables2.Table):
name = django_tables2.LinkColumn("customer-detail",
args=[django_tables2.A("pk")])
contacts = django_tables2.LinkColumn("contact-detail",
args="contacts__id",
accessor="contacts__name",
verbose_name="contacts")
class Meta:
model = Customer
sequence = ("name", "contacts")
我想要的是每个名字都链接到它的详细联系方式,但我在处理访问器的内容时出错,因此得到一个空的 table 行。
我创建列表的方法是错误的 [{"name": "Bart", "id": 1}, {"name": "Rita", "id": 7},]
还是我只是读错了有关如何访问该列表的文档?
views.py:
class CustomerListView(SingleTableView):
model = Customer
context_object_name = 'customer'
table_class = CustomerTable
template_name = "customerlist.html"
def get_queryset(self):
qs = super(CustomerListView, self).get_queryset()
return list(qs)
经过一些帮助后发现我在使用 LinkColumn
时走错了路。我使用 TemplateColumn
解决了这个问题,我想分享我的方法 - 请随时发表评论或批评:
models.py:
class ContactPerson(models.Model):
name = models.CharField('name', max_length=120)
@property
def name(self):
return self.name
class Customer(models.Model):
name = models.CharField('company name', max_length=120)
contact_persons = models.ManyToManyField(ContactPerson, blank=True)
tables.py:
class CustomerTable(django_tables2.Table):
TEMPLATE = '''
{% for contact in record.contact_persons.all %}
<a href="{% url "contact-detail" contact.pk %}">{{ contact.name }}</a><br/ >
{% endfor %}
'''
contacts = django_tables2.TemplateColumn(empty_values=(),
orderable=False,
template_code=TEMPLATE)
name = django_tables2.LinkColumn("customer-detail",
args=[django_tables2.A("pk")])
class Meta:
model = Customer
sequence = ("name", "contacts")
我有两个数据模型,company
和 contact_person
。它们以 m2m 变体链接:
models.py:
class ContactPerson(models.Model):
name = models.CharField('first name', max_length=120)
@property
def contact_name(self):
return [self.name, self.id]
class Customer(models.Model):
name = models.CharField('company name', max_length=120)
contact_persons = models.ManyToManyField(ContactPerson, blank=True, null=True)
@property
def contacts(self):
persons = []
for c in self.contact_persons.all():
persons.append({"name": c.contact_name[0], "id": c.contact_name[1]})
return persons
tables.py:
class CustomerTable(django_tables2.Table):
name = django_tables2.LinkColumn("customer-detail",
args=[django_tables2.A("pk")])
contacts = django_tables2.LinkColumn("contact-detail",
args="contacts__id",
accessor="contacts__name",
verbose_name="contacts")
class Meta:
model = Customer
sequence = ("name", "contacts")
我想要的是每个名字都链接到它的详细联系方式,但我在处理访问器的内容时出错,因此得到一个空的 table 行。
我创建列表的方法是错误的 [{"name": "Bart", "id": 1}, {"name": "Rita", "id": 7},]
还是我只是读错了有关如何访问该列表的文档?
views.py:
class CustomerListView(SingleTableView):
model = Customer
context_object_name = 'customer'
table_class = CustomerTable
template_name = "customerlist.html"
def get_queryset(self):
qs = super(CustomerListView, self).get_queryset()
return list(qs)
经过一些帮助后发现我在使用 LinkColumn
时走错了路。我使用 TemplateColumn
解决了这个问题,我想分享我的方法 - 请随时发表评论或批评:
models.py:
class ContactPerson(models.Model):
name = models.CharField('name', max_length=120)
@property
def name(self):
return self.name
class Customer(models.Model):
name = models.CharField('company name', max_length=120)
contact_persons = models.ManyToManyField(ContactPerson, blank=True)
tables.py:
class CustomerTable(django_tables2.Table):
TEMPLATE = '''
{% for contact in record.contact_persons.all %}
<a href="{% url "contact-detail" contact.pk %}">{{ contact.name }}</a><br/ >
{% endfor %}
'''
contacts = django_tables2.TemplateColumn(empty_values=(),
orderable=False,
template_code=TEMPLATE)
name = django_tables2.LinkColumn("customer-detail",
args=[django_tables2.A("pk")])
class Meta:
model = Customer
sequence = ("name", "contacts")