【发布时间】:2020-08-11 07:36:16
【问题描述】:
我有两个数据模型,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")
我想要的是每个名字都链接到它的联系方式,但我正在处理访问者的内容错误,因此得到一个空表行。
我创建列表的方法是错误的[{"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)
【问题讨论】:
标签: django django-views django-tables2