Django过滤器在一个模型中获取多个用户



我创建了一个具有推荐系统的应用程序,挑战在于我希望推荐人看到他推荐的用户的其他模型,例如。投资平台我想让推荐人看到推荐人的主账号(一个模型)和投资状态(一个模型)。

模型
class TheMain(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
main_balance = models.IntegerField(default=0)
earning_balance = models.IntegerField(default=0)
def __str__(self):
return str(self.user)

引用返回多个用户

class Referral(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
referral_balance = models.IntegerField(default=0)
code = models.CharField(max_length=12, blank=True)
referred_by = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, 
related_name="ref_by")
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.user.username}- {self.code}'
def get_referred(self):
qs = Referral.objects.all()
my_refs = []
for referred in qs:
if referred.referred_by == self.user:
my_refs.append(referred)
return my_refs

景观

referral = Referral.objects.get(user=request.user)
my_ref = referral.get_referred()
refer_main = []

refer_invest =[]
for my in my_ref:
main = TheMain.objects.filter(user = my.user)
for ma in main:
refer_main.append(ma)

print(f'this is the mama {ma} and amount funded{ma.main_balance}')

print语句返回所有引用的名称和支付的金额

Quit the server with CTRL-BREAK.
this is the mama user1 and amount funded1100
this is the mama user2 and amount funded800
this is the mama user and amount funded400.

问题是将所有这些用户循环到模板当我使用for循环

时,我得到了这个错误
TypeError at /core/referral
'TheMain' object is not iterable
Request Method: GET
Request URL:    http://localhost:8000/core/referral
Django Version: 3.2.3
Exception Type: TypeError
Exception Value:    
'TheMain' object is not iterable
Exception Location: C:UsersHPAppDataLocalProgramsPythonPython39libsite-packagesdjangotemplatedefaulttags.py, line 167, in render
Python Executable:  C:UsersHPAppDataLocalProgramsPythonPython39python.exe
Python Version: 3.9.5
Python Path:    
['C:\Users\HP\Desktop\brooker\broker',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\python39.zip',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\DLLs',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\lib',
'C:\Users\HP\AppData\Local\Programs\Python\Python39',
'C:\Users\HP\AppData\Roaming\Python\Python39\site-packages',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\win32',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\win32\lib',
'C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\Pythonwin']

但是当它不在循环中时,它只返回一个username

按要求更新视图

def referral_page(request, *args, **kwargs):
global invest_user, invest_name, invest_amount, invest_timer, main, that
referral = Referral.objects.get(user=request.user)
my_ref = referral.get_referred()
refer_main = []

refer_invest =[]
for my in my_ref:
main = TheMain.objects.filter(user = my.user)
for ma in main:
refer_main.append(ma)
print(f'this is the mama {ma} and amount funded{ma.main_balance}')
context = {
'ref': ma
}
return render(request, 'referral/referral.html', context)

的模板
<table class="table table-striped table-bordered no-margin ">
<thead>
<tr class="bg-pale-dark">
<th class="text-center border-top-0">Username</th>
<th class="text-center border-top-0">Account Status</th>
<th class="text-center border-top-0"> Date Registered</th>
<th class="text-center border-top-0">User's Investment</th>
<th class="text-center border-top-0">Investment Status</th>
<th class="text-center border-top-0">Ref Bonus</th>
</tr>
</thead>
{#                                        {{ ref }}#}
{% for r in ref %}
{{ r }}
{% endfor %}

如例外'TheMain' object is not iterable所示。您只引用了一个TheMain对象,它不是列表或查询集。将ma添加到上下文中只是将main的一个对象放入上下文中。相反,将查询集main添加到上下文。这样的

main = TheMain.objects.filter(user = my.user)
for ma in main:
refer_main.append(ma)
context = {
'ref': main
}

证实我刚才的解释,我认为refer_main变量应该在上下文中出现。

此外,我不认为有一个嵌套循环在每次迭代时都向上下文添加值是可以的。相反,在每次迭代中,将对象添加到列表中。然后将该列表添加到上下文中。最后,为什么在for loop块中返回渲染?

相关内容

  • 没有找到相关文章

最新更新