I’ve got a small problem I can’t seem to figure out.
I’ve got the following model:
class UserWhale(models.Model):
currencyPair = models.ForeignKey('currencies.CurrencyPair', verbose_name='Currency Pair')
currency = models.ForeignKey('currencies.Currency', verbose_name='Source Currency')
currencyTarget = models.ForeignKey('currencies.Currency', related_name='whale_currency_target_rel', verbose_name='Target Currency')
userBankAccount = models.ForeignKey(UserBankAccount, verbose_name='Deposit Bank Account')
whale_fee_percentage = models.DecimalField(max_digits=11, decimal_places=2)
whale_amount = models.DecimalField(max_digits=11, decimal_places=2)
whale_matched = models.DecimalField(max_digits=11, decimal_places=2)
priority = models.IntegerField()
user = models.ForeignKey('auth.User', related_name='whale_relation_user', editable=False)
created_by = models.ForeignKey('auth.User', editable=False)
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
history = audit.AuditTrail()
When I output the following:
{{ request.user.userwhale_set.count }}
It always outputs as zero even tho the current user has data in that table. The “user” and “created_by” fields are always different so I’m wondering whether or not the code above is using the “created_by” relationship instead of the “user” relationship. If so, how do I specify which one to use?
Many thanks
Ben.
You have specified the
related_nameproperty on theuserforeignkey, so you should call that name:request.user.whale_relation_user.count.