简体   繁体   中英

Combining the required fields from two different models

I am implementing django application where I have two models file and filepage in which filepage model has a foreign key file.

currently my view looks like this:

Container     P        Bucket_type

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

I want to convert it into:

Container      P           Bucket_type

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

the container name is field in my container model and student and bucket type are in bucket model.

Updated:

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)

currently I am trying to write a function which combines this records but it is taking time to make that list of combinations as I have bulk of records in my database.

Is there any way to create this data using Django's third party packages or any other solution?

Your Bucket model should look like this

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)

You can Try to create Charfield or Charfields in YouModel:

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.

For Most convenient construction i need to Ask You "What You Want?", and comment You post. But i can t do this because i havn t 50 REPUTATION.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM