如何从Django中的auth_user表中检索Email-ID?

时间:2017-04-02 22:21:37

标签: django django-1.5

如何从Django中的auth_user表中检索Email-ID?  如果我访问auth_user表,我只获得id和用户名。

Models.py

class AuthUser(models.Model):
id = models.IntegerField(primary_key=True)
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField()
    is_superuser = models.BooleanField()
    username = models.CharField(max_length=30, unique=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=75)
    is_staff = models.BooleanField()
    is_active = models.BooleanField()
    date_joined = models.DateTimeField()

class Meta:
    db_table = 'auth_user'

def as_json(self):
    return dict(
        user_id=self.id,
        username=self.username,
        email=self.email
    )

views.py

class UserViewSet(generics.ListCreateAPIView):
    serializer_class = AuthUserSerializer

    def get_queryset(self):
        q = self.kwargs['username']
        if q != '*':
            return AuthUser.objects.filter(username=q)
        else:
            return AuthUser.objects.all()

urls.py

url(r'^getUserData/(?P<username>.+)/$',
                                                  views.UserViewSet.as_view(),
                                                  name='user-details'),

我只需要来自AuthUser对象的电子邮件

1 个答案:

答案 0 :(得分:0)

您是否创建了模板?您的视图类应指定要用于呈现视图的html文件

即。

template_name = 'app/template.html'

查看文档中的示例: https://docs.djangoproject.com/en/1.10/topics/class-based-views/generic-display/

然后,您可以在模板中包含以下代码以列出用户电子邮件

     <ul>
        {% for user in object_list %}
            <li>{{ user.email }}</li>
        {% endfor %}
    </ul>