1

When retrieving a user object set up as below:

class User(models.Model):
    """
    User model
    """
    id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
    email = models.EmailField(max_length=255, null=True, default=None)
    password = models.CharField(max_length=255, null=True, default=None)
    ACCOUNT_CHOICE_UNSET = 0
    ACCOUNT_CHOICE_BRAND = 1
    ACCOUNT_CHOICE_CREATOR = 2
    ACCOUNT_CHOICE_AGENCY = 3
    ACCOUNT_CHOICES = (
        (ACCOUNT_CHOICE_UNSET, 'Unset'),
        (ACCOUNT_CHOICE_BRAND, 'Brand'),
        (ACCOUNT_CHOICE_CREATOR, 'Creator'),
        (ACCOUNT_CHOICE_AGENCY, 'Agency'),
    )
    account_type = models.IntegerField(choices=ACCOUNT_CHOICES, default=ACCOUNT_CHOICE_UNSET)

    class Meta:
        verbose_name_plural = "Users"

    def __str__(self):
        return "%s" % self.email

In the following manner:

        try:
            user = User.objects.get(email=request.data['email'])
        except User.DoesNotExist:
            response_details = {
                    'message': "Invalid email address.",
                    'code': "400",
                    'status': HTTP_400_BAD_REQUEST
            }
            return Response(response_details, status=response_details['status'])

Why can I only view the email property when trying to print it out? I need to be able to access the account_type property as well.

    if user:
        print(user)
methuselah
  • 12,766
  • 47
  • 165
  • 315

1 Answers1

5

You can modify model's __str__ method for this:

def __str__(self):
    return "%s %s" % (self.email, self.account_type)

To leave only email on admin page you need to customize admin class. Add this to admin.py file:

from django.contrib import admin
from .models import User

class UserAdmin(admin.ModelAdmin):
    list_display = ('email')

admin.site.register(User, UserAdmin)
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • Ok this works but how do I override it in the admin screen (http://127.0.0.1:8000/admin/api/user/) so that only the email address appears i.e. `test@hotmail.co.uk` instead of `test@hotmail.co.uk 3` – methuselah Apr 30 '18 at 15:34
  • @methuselah you can specify `list_display` attribute on admin class, to include which fields will be displayed. See details here: https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display – neverwalkaloner Apr 30 '18 at 15:40
  • @methuselah `UserAdmin` should be added to `admin.py` file. Check this doc: https://medium.com/django-musings/customizing-the-django-admin-site-b82c7d325510 – neverwalkaloner Apr 30 '18 at 15:45
  • @methuselah I've updated my answer to show you what you need to add to `admin.py`. – neverwalkaloner Apr 30 '18 at 15:49
  • Thanks. This helped. Lastly, how do I access the properties in the user object? Right now `print(user)` returns something like a string... how I do I get it to return an object so I can look up the properties with a key? – methuselah Apr 30 '18 at 15:56
  • @methuselah try this: `print(user.__dict__)`. More details here: https://stackoverflow.com/questions/21925671/convert-django-model-object-to-dict-with-all-of-the-fields-intact – neverwalkaloner Apr 30 '18 at 15:58
  • 1
    Thanks this has really helped! – methuselah Apr 30 '18 at 16:03