Skip to content

Instantly share code, notes, and snippets.

@nck2
Last active July 20, 2017 06:25
Show Gist options
  • Save nck2/ac7d813751f6869cb181271ef767c5da to your computer and use it in GitHub Desktop.
Save nck2/ac7d813751f6869cb181271ef767c5da to your computer and use it in GitHub Desktop.
외래키 접근 방법 질문
# models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
phone_number = models.CharField(max_length=20, verbose_name="전화번호")
address = models.CharField(max_length=30, verbose_name="분반")
# views.py
@login_required
def profile(request,userid):
User=get_user_model()
me= User.objects.get(id=userid)
mypro = Profile.objects.filter(user=me)
print('mypro',mypro) # 출력결과: mypro <QuerySet []>
print('me',me) # 출력결과: me testuser
return render(request, 'accounts/profile.html',{'mypro':mypro,})
#urls.py
url(r'^profile/(?P<userid>[0-9]+)/$', views.profile, name='profile')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment