2
You’ve used the class name as an attribute, so I think you’ll find it really helpful to set the related_name
param on fields like this.
For example;
class User_Info(models.Model):
user = models.OneToOneField(User, null=True, related_name='info', on_delete = models.CASCADE)
You can then access that in a view by doing;
def userPage(request):
user = request.user
user_info = user.info
context = {
"user" : user,
"user_info" : user_info,
}
return render(request, "accounts/user.html", context)
You can read the docs on related_name
here; https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.ForeignKey.related_name
1
The default name of the relation in reverse is the name of the class with lowercase, so you access it with user_info
:
def userPage(request):
info = user.user_info
trades = info.trade_set.all()
total_trades = trades.count()
context = {'_user_' : info, 'trades' : trades, 'total_trades' : total_trades}
return render(request, "accounts/user.html", context)
Or you can set the related_name=…
parameter [Django-doc] in the OneToOneField
to specify the name of the relation in reverse.
0
here are a few things I’d suggest. The code you have listed is trying to connect two classes together, not instances. So I would put a related_name='user_info'
on your User_Info class, then you can call something like user.user_info.
I strongly recommend not using underscores in class names.
Also, allowing null = True
for the relationship to User on a user annotation class seems very weird to me. User not having a User_Info instance is ok. User-Info instance without a User makes no sense to me.
- [Django]-Celery beat process running on Heroku sends task twice
- [Django]-How to do something when I change a field in Django admin for a model?
- [Django]-A correct case to use Django signals