结合两个不同模型中的必填字段

时间:2016-04-07 04:17:31

标签: django python-3.x

我正在实现django应用程序,其中我有两个模型文件和filepage,其中filepage模型有一个外键文件。

目前我的观点如下:

Container     P        Bucket_type

f1      p1        doc1
f1      p2        doc2
f1      p3        doc3
f2      p4        doc4
f2      p5        doc5

我想将其转换为:

Container      P           Bucket_type

f1    p1,p2,p3     doc1,doc2,doc3
f2    p4,p5        doc4,doc5

容器名称是我的容器模型中的字段,学生和存储桶类型是桶模型。

更新:

class Containers(models.Model):
    name = models.CharField(max_length=30)


class Bucket(models.Model):
    file = models.ForeignKey(Container, on_delete=models.CASCADE, related_name='buckets')
    bucket_number = models.IntegerField()
    p = models.ForeignKey(P, null=True, blank=True)
    bucket_type = models.ForeignKey(D, null=True, blank=True)

目前我正在尝试编写一个结合了这些记录的函数,但由于我的数据库中包含大量记录,因此需要时间来制作组合列表。

有没有办法使用Django的第三方软件包或任何其他解决方案创建这些数据?

2 个答案:

答案 0 :(得分:1)

您的Bucket模型应如下所示

class Bucket(models.Model):
    file = models.ForeignKey(Container, on_delete=models.CASCADE, related_name='buckets')
    bucket_number = models.IntegerField()
    student = models.ManyToManyField(Student, null=True, blank=True)
    bucket_type = models.ManyToManyField(BucketType, null=True, blank=True)

答案 1 :(得分:1)

您可以尝试在YouModel中创建Charfield或Charfield:

patient1 = FilePage.objects.order_by('patient')[0]  # --  [0] or other variant
document1 = FilePage.objects.order_by('document_type')[0]  # --  [0] or other variant
YouModel.objects.order_by('charfield').update(charfield=patient1) # Something like this construction.

对于最方便的建设我需要问你“你想要什么?”,并发表评论你的帖子。但我可以t do this because i havn告知50。

相关问题