We would need to see your complete ModelAdmin class for that list_display to be able to answer that question.
As you will see, lastName and firstName are not fields. They are methods in the admin class. I am struggling to find a way to get these fields into the admin section. Is there some other way? I think that they need to be fields in the model to be able to sort. Is that correct?
admin.py
# ───────────────────────────────────────────────────────────────────────────────
# Boston Pizza Orders
# ───────────────────────────────────────────────────────────────────────────────
@admin.register(BostonPizzaOrder)
class BostonPizzaOrder(admin.ModelAdmin):
# def firstName(self, obj):
# return obj.user_name.first_name
# def lastName(self, obj):
# return obj.user_name.last_name
def firstName(self, Profile):
return Profile.user_name.firstName
def lastName(self, Profile):
return Profile.user_name.lastName
# Fields and section appearing in the change list. Note: The fileds listed
# here must be editible. orderSubmitted_Date is not and can't be listed here.
# it can be in the list_display because it's only viewed.
fieldsets = (
('Order Name and Order Number', {
'fields': [('user_name', 'order_Number',)],
# 'description': '%s' % TEXT,
}),
('Pasta Choice', {
'fields': [('bpPasta',)]
}),
('Salad Choice', {
'fields': [('bpSalad', 'bpSaladDressing'),
# ('', '')
],
# 'classes': ('collapse',), <collaps the Veggies section if you like>
}),
('Drink Choice', {
'fields': [('bpDrink')]
}))
radio_fields = {
# 'user_name':admin.VERTICAL,
'bpPasta':admin.VERTICAL,
# '':admin.VERTICAL,
'bpSalad':admin.VERTICAL,
'bpSaladDressing':admin.VERTICAL,
# # 'cheese':admin.VERTICAL, # ManyToManyField
# 'bread_size':admin.VERTICAL,
# # 'extra':admin.VERTICAL, # ManyToManyField
# 'drink':admin.VERTICAL,
# 'cookie':admin.VERTICAL,
# 'chips':admin.VERTICAL,
}
list_display = (
'user_name',
'lastName',
'firstName',
'order_Number',
'bpPasta',
'bpSalad',
'bpSaladDressing',
'bpDrink',
'orderSubmitted_Date',
)
ordering = ('user_name',)
# ordering = ('lastName',)
search_fields = ('bpPasta',)
list_filter = ('order_Number',)
Excerpt from view.py : Please excuse all my notes in the file. I wanted to highlight the variable used in userSelection. I would like to add another variable to sort on last name. This is my problem. Where to get the last name from User or Profile. I think Profile allows more flexibility in that I can add more fields.
# ───────────────────────────────────────────────────────────────────────────────
# Boston Pizza Group Meal Order
# ───────────────────────────────────────────────────────────────────────────────
@login_required(login_url='/login')
def BostonPizzaGroupOrder(request):
submitted = False
form = BostonPizzaGroupOrderForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user_name = request.user.profile
# instance.user_name = request.user.profile.lastName
# user_name = request.user
print('User: ' f'{ instance.user_name}')
print('Profile Last Name: ' f'{ instance.user_name.lastName}')
print('Profile first Name: ' f'{ instance.user_name.firstName}')
# Returns a data dictionary.
print('form.cleaned_data: ' f'{form.cleaned_data}')
# Returns the actual value from the dictionary.
userSelection = form.cleaned_data['order_Number']
print('UserSelection: ' f'{userSelection}')
# This works too.
# print(form.cleaned_data['order_Number'])
# Check to see if the form is bound, returns true if bound.
print('form.is_bound: ' f'{form.is_bound}')
OrderCount = BostonPizzaOrder.objects.all().filter(order_Number=userSelection).count
print(userSelection)
# lastName = user.user_name.first_name
orderByNumber = BostonPizzaOrder.objects.all().filter(order_Number=userSelection).order_by('user_name')
# orderByNumber = BostonPizzaOrder.objects.all().filter(order_Number=userSelection).order_by('bpPasta')
return render(request, 'meals/BostonPizzaGroupOrder.html',
{'title': 'Group Order',
'MyOrders': orderByNumber,
'OrderCount': OrderCount,
'form': form,
})