gpt4 book ai didi

Django 教程 selection_set

转载 作者:行者123 更新时间:2023-12-03 10:49:53 26 4
gpt4 key购买 nike

来自 Django 教程:

我已经定义了我的模型如下:

from django.db import models
import datetime
from django.utils import timezone

# Create your models here.

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self): # Python 3: def __str__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self): # Python 3: def __str__(self):
return self.choice_text

choice_set 在哪里定义以及它是如何工作的?
>>> p = Poll.objects.get(pk=1)

# Display any choices from the related object set -- none so far.
>>> p.choice_set.all()

最佳答案

我不知道你想要多深的解释,但是当你做的时候 Django 会为你定义 poll = models.ForeignKey(Poll) .

You can read here about it.

关于Django 教程 selection_set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17566031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com