如何从数据库

时间:2017-06-30 06:38:47

标签: python django django-models django-templates

用户登录我的django应用程序后。用户可以在数据库中戳另一个用户。如何获取扑克的用户名和他们给数据库中特定用户的戳数?以下是我的代码。

这就是我获取用户在html模板中被戳的次数。请帮忙。 {{i.pokegotten.all |长度}}

    class User(models.Model): 
         firstname = models.CharField(max_length=45)
         lastname = models.CharField(max_length=45)
         email = models.EmailField(max_length=45)
         password = models.CharField(max_length=255)
         bday = models.DateField(auto_now=True)
         created_at = models.DateTimeField(auto_now_add = True)
         updated_at = models.DateTimeField(auto_now = True)
         objects = UserManager()

    class Poke(models.Model):  
         poker = models.ForeignKey(User,related_name= "pokegiver")
         poked = models.ForeignKey(User, related_name= "pokegotten")
         created_at = models.DateTimeField(auto_now_add = True)
         updated_at = models.DateTimeField(auto_now = True)
         objects = PokeManager()

1 个答案:

答案 0 :(得分:0)

获得poke giver firstname,(从您的问题来看,您可能有多次与扑克相同的用户)

get_firstName = Poke.objects.filter(pokegiver=request.user)
if get_firstName:
    firstName = get_firstName[0].pokegiver.firstname

类似地, 如果它是用户特定的:

get_poked_count = Poke.objects.filter(pokegotten="user object").count()

得到全部数,

get_poked_count = Poke.objects.all().count()