Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - how to call property method from model class to html in django

Im making a django app, and its basically an admin site, i have an app called calculator, inisde it i have 3 models Transaction, FamilyGroup and FamilyMember, each model has some property methods for calculation purposes. here are the models for more clearness :

class Transaction(models.Model):
    chp_reference = models.CharField(max_length=50, unique=True)
    rent_effective_date = models.DateField(null=True, blank=True)
    income_period = models.CharField(max_length=11)                                         
    property_market_rent = models.DecimalField(max_digits=7)

    @property
    def ftb_combined(self):
        ftb_combined = 0
        for family_group in self.familygroup_set.all():
            ftb_combined += family_group.ftb_combined
        return ftb_combined

class FamilyGroup(models.Model):

    name = models.CharField(max_length=10)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
    last_rent = models.DecimalField(max_digits=7)

   @property
   def additional_child_combined(self):
       return (self.number_of_additional_children
               or 0) * self.maintenance_rate_additional_child
class FamilyMember(models.Model):
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
    family_group = models.ForeignKey(FamilyGroup, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=100, null=True, blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    income = models.DecimalField(max_digits=6)

   @property
   def weekly_income(self):
       if self.transaction.income_period == 'Weekly':
           return self.income
       return (self.income or 0) / 2

this is how my models are connected, now i made a method in views.py as below:

def transaction_print(request, transaction_id):
    transaction = Transaction.objects.get(id=transaction_id)
    
    return render(request, 'report.html', {'transaction':transaction})

I want to make a report in report.html, 1 report for each transaction, and the transaction can have many FamilyGroups and FamilyMember, and will include almost all the data from the models and the property methods inside it. here what i thought in the report.html

<table class="table">

    <thead class="thead-dark">
        <tr>
            <th>CHP Reference </th>
            <th>Rent Effective From (dd/mm/yyyy)</th>
            <th>CRA Fortnightly Rates valid for 6 months from</th>
            <th>Market Rent of the Property </th>
            <th>Number of Family Groups </th>
        </tr>

    </thead>
    <tbody>
        <tr>
            <td>{{ transaction.chp_reference }} </td>
            <td>{{ transaction.rent_effective_date }} </td>
            <td>0</td>
            <td>{{ transaction.property_market_rent }}</td>
            <td>{{ transaction.number_of_family_group }}</td>

        </tr>

    </tbody>
   {% for family_group in transaction.family_group_set.all %} ??
   {% for m in family_group.transaction.family_group_set.all %} ??
</table>

Im really not sure how to perform the nested loop to iterate through the FamilyGroup and FamilyMember inside the transaction report.html would appreciate a hint how this be done.

question from:https://stackoverflow.com/questions/65926869/how-to-call-property-method-from-model-class-to-html-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the documentation Django sets the name to MODELNAME_set. However you can still use the related_name property to set a name for your backward reference (you will still be able to use MODELNAME_set as well).

Here's how to achieve it using related_name:

models.py

class FamilyGroup(models.Model):

    name = models.CharField(max_length=10)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="family_groups") # Notice the related_name here as it will be used later on
    last_rent = models.DecimalField(max_digits=7)
    # ...

class FamilyMember(models.Model):
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="family_members") # Notice the related_name
    family_group = models.ForeignKey(FamilyGroup, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=100, null=True, blank=True)
    date_of_birth = models.DateField(null=True, blank=True)
    income = models.DecimalField(max_digits=6)
    # ...

Now you can loop through them like so:

report.html

{% for family_group in transaction.family_groups.all %}
{{ family_group.name }}
{% endfor %}

{% for family_member in transaction.family_members.all %}
{{ family_member.name }}
{% endfor %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...