假設你有一個用戶一個OneToOne /獨特ForeignKey的關係的Django模型,如秀上Django documentation on how to create a UserProfile.:什麼是Django模型的情況下,與用戶
現在讓我們假設你有一個視圖方法,它接受一個你可以從中獲取用戶的請求。查詢與該用戶關聯的配置文件的最佳方式是什麼?
from django.contrib.auth.models import User
# sample user profile model associated with user
class UserProfile(models.Model):
likes_spam = models.BooleanField()
user = models.OneToOneField(User)
#view method
def forward_to_practice_home(request):
user = request.user
profile_for_user = #insert code here that would get the profile for that user