I have 4 models that are related to my User model. 3 of them are One-To-One relationships and 1 is a Foreign Key relationship. I want to present all fields from the 5 models in a class based detail view. I can successfully access the fields of the user model and the 3 models with One-To-One relationships, but the model with Foreign Key relationship (userprofilefiles) throws "AttributeError: ‘User’ object has no attribute ‘userprofilefiles’.
class ReviewDetail(DetailView):
template_name = 'review/review_detial.html'
def get_object(self, queryset=None):
user = User.objects.get(pk=self.kwargs.get('pk'))
print(user)
print(user.userprofile) # One-To-One
print(user.userprofilequestions) # One-To-One
print(user.userprofileagreement) # One-To-One
print(user.userprofilefiles) # Foreign Key
return user
I have also tried to get the failing model as a separate object, which works but does not get passed to the template.
class ReviewDetail(DetailView):
template_name = 'review/review_detial.html'
def get_object(self, queryset=None):
user = User.objects.get(pk=self.kwargs.get('pk'))
print(user)
print(user.userprofile) # One-To-One
print(user.userprofilequestions) # One-To-One
print(user.userprofileagreement) # One-To-One
upload = UserProfileFiles.objects.get(pk=self.kwargs.get('pk'))
print(upload) # Foreign Key
return user, upload
Is there any way to get one of these 2 methods working properly?