简体   繁体   English

django视图,获取请求并将其用作参数

[英]django views, getting request and using it as a parameter

I'm very confused about this right now, so I know when there's a simple code like the below 我现在对此很困惑,所以我知道何时有如下所示的简单代码

def text_detail(request ,course_pk, step_pk):
    step = get_object_or_404(Text, course_id = course_pk, pk=step_pk)

course_pk and step_pk from the url, and those requests are set equal to course_id and pk here. 网址中的course_pk和step_pk,这些请求在此处设置为等于course_id和pk。 but what I don't understand is what is course_id and pk here? 但是我不明白的是这里的course_id和pk? I mean, course_id is from Course model which is foreignkey to step. 我的意思是,course_id来自课程模型,这是外键。 so it's self.Course.id so it's course_id. 所以它是self.Course.id,所以是course_id。 But then, how about the next one pk? 但是,下一个pk呢? shouldn't it be step_id = step_pk? 不应该是step_id = step_pk? when it's just pk how does django know which pk it is? 当只是pk时,django怎么知道它是哪个pk? Sorry if the question is very confusing, I'm very confused right now. 很抱歉,如果这个问题很令人困惑,我现在很困惑。

Edit 编辑

class Step(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField()
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course)

    class Meta:
        abstract = True
        ordering = ['order',]
    def __str__(self):
        self.title
class Text(Step):
    content = models.TextField(blank=True, default="")

Actually the get_or_404() method doing a similar/exact job as below, 实际上, get_or_404()方法完成了以下类似/精确的工作,

try:
    return Text.object.get(pk=step_pk,course_id = course_pk)
except Text.DoesNotExist:
    raise Http404

You can read the source code of the same here 您可以在此处阅读相同源代码

What is course_id and pk ? 什么是course_idpk
Both are attributes of your Text model, as the name indicates pk is your Primary Key of Text model and course_id is the id / pk of course field which is a FK. 两者都是Text模型的属性,顾名思义pkText模型的主键,而course_idcourse字段的id / pk ,即FK。

EDIT 编辑
Text is inherited from Step model so, it will show properties of usual python class.Hence, the Text model be like this internally (not-exact) Text是从Step模型继承的,因此它将显示普通python类的属性。因此, Text模型在内部是这样的(不精确)

class Text(models.Model):
    content = models.TextField(blank=True, default="")
    title = models.CharField(max_length=200)
    description = models.CharField()
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course)
    class Meta:
        ordering = ['order', ]
    def __str__(self):
        return self.title



Example

text = Text.objects.get(id=1) # text instance with id=1
text.course_id # will hold the id of "course" instance which is related to the particular "text" instance


URL assignment and all those stuffs are entirely depends on your choice and logic. URL分配以及所有这些东西完全取决于您的选择和逻辑。 So If you need to get a Text instance in your view, do as below, 因此,如果您需要在视图中获取Text实例,请执行以下操作,

text = get_object_or_404(Text, pk = pk_of_TEXT_instance)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM