my model
class user_profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
age = models.IntegerField()
profile_created = models.DateTimeField(auto_now_add=True, auto_now=False)
timestamp = models.DateTimeField(auto_now=True, auto_now_add=False)
admin.py
class UserProfileAdmin(admin.ModelAdmin):
list_display = ['user','user.username','profile_created', 'timestamp']
admin.site.register(user_profile, UserProfileAdmin)
It show the following error(s):
ERRORS:
<class 'testapp.admin.UserProfileAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'user.username', which is not a call
able, an attribute of 'UserProfileAdmin', or an attribute or method on 'testapp.user_profile'.
How can I fetch another table values in admin.py
?
As per the PEP8, class names should normally use the CapWords convention.
class UserProfile(models.Model):
# your code
also, to show the username in DjangoAdmin, you should define a method as,
from django.core.exceptions import ObjectDoesNotExist
class UserProfileAdmin(admin.ModelAdmin):
list_display = ['user', 'username', 'profile_created', 'timestamp']
def username(self, instance): # name of the method should be same as the field given in `list_display`
try:
return instance.user.username
except ObjectDoesNotExist:
return 'ERROR!!'
admin.site.register(UserProfile, UserProfileAdmin)