我有一些模特以父母-孩子-孩子的孩子的方式。在我的views.py中,我做到了
swsallsteps = sws_dcoument_step.objects.filter().prefetch_related(my various models)
我遇到的问题是如何从相关信息中预取相关信息?我在这部分迷路了。示例我的views.py
swsallsteps = SWS_Document_Step.objects\
.filter(document_number=document_id)\
.prefetch_related('swes_step_set', 'sws_step_hazard_set', 'sws_step_ppe_set')
这恰好返回了我的期望。与SWS_Document_Step关联的SWES_Step_Set类似于下面的前两个模型。但是,如何拉出与SWES_Document_Step关联的SWES_Step_Picture模型?我想这比我做的要简单得多。
以下示例为模型。
class SWS_Document_Step(models.Model):
STEP_TYPE_CHOICES = (
('People', 'People'),
('Quality', 'Quality'),
('Velocity', 'Velocity'),
('Cost', 'Cost'),
)
document_number = models.ForeignKey(SWS_Document, on_delete=models.CASCADE)
sws_sequence_number = models.PositiveIntegerField(editable=True, null=True)
sws_work_element_description = models.CharField(max_length=500)
sws_step_type = models.CharField(max_length=30, choices=STEP_TYPE_CHOICES, null=True, blank=True)
pub_date = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'SWS Document Step'
verbose_name_plural = '005 SWS Document Steps'
def __str__(self):
return str(self.document_number) + " " + str(self.sws_sequence_number) + " " + str(self.sws_work_element_description)
class SWES_Step(models.Model):
STEP_TYPE_CHOICES = (
('People', 'People'),
('Quality', 'Quality'),
('Velocity', 'Velocity'),
('Cost', 'Cost'),
)
sws_document_id = models.ForeignKey(SWS_Document_Step, on_delete=models.CASCADE, null=True)
swes_description = models.CharField(max_length=500)
swes_step_type = models.CharField(max_length=8, choices=STEP_TYPE_CHOICES, blank=True)
pub_date = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'SWES Step'
verbose_name_plural = '012 SWES Document Steps'
def __str__(self):
return str(self.sws_document_id) + " " + str(self.swes_description)
class SWES_Step_Picture(models.Model):
swes_step = models.ForeignKey(SWES_Step, on_delete=models.CASCADE)
sws_step_photo = models.ImageField()
pub_date = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = 'SWES Step Picture'
verbose_name_plural = '015 SWES Step Pictures'
答案 0 :(得分:0)
IDentityUser
抱歉,不禁重命名了您的模型,它们只是不执行操作的示例:
1。Don't使用>>> from django_single.models import *
>>> SWSDocumentStep.objects.select_related('document_number').prefetch_related('swesstep_set', 'swesstep_set__swessteppicture_set')
DEBUG (0.004) SELECT "django_single_swsdocumentstep"."id", "django_single_swsdocumentstep"."document_number_id", "django_single_swsdocumentstep"."sws_sequence_number", "django_single_swsdocumentstep"."sws_work_element_description", "django_single_swsdocumentstep"."sws_step_type", "django_single_swsdocumentstep"."pub_date", "django_single_swsdocument"."id", "django_single_swsdocument"."name" FROM "django_single_swsdocumentstep" INNER JOIN "django_single_swsdocument" ON ("django_single_swsdocumentstep"."document_number_id" = "django_single_swsdocument"."id") WHERE "django_single_swsdocumentstep"."id" = 10 LIMIT 21; args=(10,)
DEBUG (0.001) SELECT "django_single_swesstep"."id", "django_single_swesstep"."sws_document_id", "django_single_swesstep"."swes_description", "django_single_swesstep"."swes_step_type", "django_single_swesstep"."pub_date" FROM "django_single_swesstep" WHERE "django_single_swesstep"."sws_document_id" IN (10); args=(10,)
DEBUG (0.002) SELECT "django_single_swessteppicture"."id", "django_single_swessteppicture"."swes_step_id", "django_single_swessteppicture"."pub_date" FROM "django_single_swessteppicture" WHERE "django_single_swessteppicture"."swes_step_id" IN (10); args=(10,)
<QuerySet [<SWSDocumentStep: SWSDocument object (10) None >]>
并将ForeignKey
添加到模型的末尾,例如:_id
> sws_document_id
2。在python中,我们不会在类中的单词之间添加sws_document
,例如:_
> SWES_Step_Picture
SWESStepPicture
# to get this to work you will need to do the following steps
mkdir django_simple/migrations
touch django_simple/django_simple.py
# copy and paste the content of the file bellow in django_simple/django_simple.py
python django_simple/django_simple.py makemigrations
python django_simple/django_simple.py migrate
python django_simple/django_simple.py shell -c 'from django_simple.models import *; SWESStepPicture.objects.create(swes_step=SWESStep.objects.create(sws_document=SWSDocumentStep.objects.create(document_number=SWSDocument.objects.create(name="blah"))))'