English 中文(简体)
在Django的辅导中有什么选择?
原标题:What is choice_set in this Django app tutorial?

The line in the Django tutorial, 撰写第一份《詹戈评论》,第一部分::

p.choice_set.create(choice= Not much , votes=0)

How is choice_set 要求存在,什么?

缩略语 您能够阐述?

<><>UPDATE: http://stackoverflow.com/users/189179/ben-james” 回答:

最佳回答
问题回答

在此提出了两个关键问题。 <>First: How is choice_set calls into existence. <>秒钟>: 是什么?

对于像我这样的所有新发展者,让我描述我如何使我容易。 让我首先回答第二个问题。 通过这三字“是什么?” Modelase, Object-set related to that instance, Related_manager

Django 辅导模型:

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField( date published )


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

<>Instance:

q = Question.objects.get(pk=3)
# Here q is an instance of model class  Question .

c = Choice.objects.get(pk=32)
# Here c is an instance of model class  Choice .

 Model Instance  is a single  ROW  of an entire  TABLE  of your database

此处的<编码> 问题示范作为<代码>foreign key至 选择模型。 因此,所有objects-set与e.q有关 可以通过使用:

q.choice_set.all()

因此,choice_set,此处指与问题有关的所有选择,即:pk=3。

现在,第一个问题的答复需要第三字 Relatd Manager。 Django 文件here:-

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.

可以用外在钥匙中的相关“姓名参数”来改变这个词(组)。

question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name="choices")

外国钥匙的后向关系:

q.choice_set.all()
# If using related_name, then it is same as 
q.choices.all()

# All the choices related to the instance q.

前瞻性关系:

choice_qs = Choice.objects.all()
choice_qs.filter(question=q)
# Same result as above. All the choices related to instance q.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...