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” 回答:。
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” 回答:。
页: 1
因此,每条<代码>Choice明确有一个question
领域,你在模型中宣布了这一点。
Django s ORM沿用了 问题
的后向关系,自动生成一个称为foo_set
的外地,其中
如果你不喜欢<代码>foo_set naming,而Django选择的是自动的,或者如果你对同一模式有不止一个外国钥匙并且需要加以区别,则你可使用ForeignKey。
在此提出了两个关键问题。 <>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.
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ]="...