[Answer]-Django struggling with the basics reportlab

1👍

Here is a suggestion how you can go about this, first some code and then some explanation:

HOUSE_X = ...
PARENT_X = ...
CHILD_X = ...
START_Y = ...
SEP_Y = ...

y = START_Y;
houses = House.objects.all()
for house in houses:
    p.drawString(HOUSE_X, y, house.address);
    y += SEP_Y
    parents = Parent.objects.filter(house=house)
    for parent in parents:
        p.drawString(PARENT_X, y, parent.name);
        y += SEP_Y
        children = Child.objects.filter(parent=parent)
        for child in children:
            p.drawString(CHILD_X, y, child.name);
            y += SEP_Y

Ok, so first of, the magic values at the top defines the x position of each model, my suggestion is that HOUSE_X < PARENT_X < CHILD_Xto get a identation but it’s up to you. Next, START_Y is where we start from y and SEP_Y is the line height that will be increased for every drawn string, you will see y is increased after every call to drawString.

Now I just looped through all the models, getting their related objects and printed those, using my magic values and increasing y at every step.

Hopefully this gives you a start. From here you can decide the magic values and decide exactly what should be printed on every line.

Leave a comment