Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

many to many in django-tables2

I have django's models with many to many through ForeignKey:

    class A(m.Model):
        id = m.AutoField(primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)

        def __str__(self):
            return self.name


    class B(m.Model):
        id = m.AutoField(primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)
        a = m.ForeignKey(A)

        def __str__(self):
            return self.name


    class C(m.Model):
        id = m.IntegerField(null=False, unique=True, primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)
        bs = m.ManyToManyField(B, through='D')

        def __str__(self):
            return '%d, %s, (%s), (%s)' % (
                self.id,
                self.name, 
                ', '.join(b.name for b in self.bs.all()), 
                ', '.join(b.a.name for b in self.bs.all()))
                )


    class D(m.Model):
        c = m.ForeignKey(C)
        b = m.ForeignKey(B)

        class Meta:
            unique_together = ('c', 'b')

django-tables2 from model C:

class CTable(tables.Table):

    class Meta:
         model = C

views:

def C(request):
    data = C.objects.all()
    c_table = t.CTable(data)
    return render(request, 'c.html', {'c_table': c_table})

and in c.html:

...
    {% render_table c_table %}
...

I get table with only two columns (id, name) instead four (id, name, b.name, b.a.name). How to get the missing columns from many to many? Sorry for my terrible english.

like image 398
pirr Avatar asked Feb 11 '23 00:02

pirr


1 Answers

Add a property method in the model and render it using table class, ie:

Model class

class C(m.Model):
    id = m.IntegerField(null=False, unique=True, primary_key=True)
    name = m.CharField(max_length=250, unique=True, null=False)
    bs = m.ManyToManyField(B, through='D')

    def __str__(self):
        return '%d, %s, (%s), (%s)' % (
            self.id,
            self.name, 
            ', '.join(b.name for b in self.bs.all()), 
            ', '.join(b.a.name for b in self.bs.all()))
            )

   @property
   def all_bs(self):
      return ', '.join([x.name for x in self.bs.all()])

Table class

class CTable(tables.Table):

    class Meta:
         model = C
         fields = ('a', 'b', 'all_bs')
like image 161
ruddra Avatar answered Feb 15 '23 10:02

ruddra