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.
Add a property method in the model and render it using table class, ie:
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()])
class CTable(tables.Table):
class Meta:
model = C
fields = ('a', 'b', 'all_bs')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With