How do I retrieve Email-ID from auth_user table in Django?

1.7k views Asked by At

How do I retrieve Email-ID from auth_user table in Django? I am getting only id and username if I access the auth_user table.

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'),

I need only email from AuthUser object

1

There are 1 answers

2
kujosHeist On

Have you created a template? Your view class should specify which html file you want to use to render the view

i.e.

template_name = 'app/template.html'

Check out the example here in the docs: https://docs.djangoproject.com/en/1.10/topics/class-based-views/generic-display/

You can then have the following code in your tamplate to list the user emails

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