如何使用user id
获取student id
。我的模型名为Student
,如下所述:
class Student(models.Model):
user = models.ForeignKey(User)
nationality = models.CharField(max_length=255, blank=True, null=True)
答案 0 :(得分:1)
试试这个:
student = Student.objects.first()
# or this
student = Student.objects.get(id=34)
user_id = student.user.id
# or this
user_id = student.user_id
我建议你阅读Django Tutorial,它回答了大部分问题。