【问题标题】:How to access elements of list in accessor如何在访问器中访问列表元素
【发布时间】:2020-08-11 07:36:16
【问题描述】:

我有两个数据模型,companycontact_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


    【解决方案1】:

    经过一些帮助后发现,我在使用 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")
    

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      相关资源
      最近更新 更多