У меня есть модель под названием partnershipArm, когда новая модель создается без сигнала после сохранения, она работает очень быстро.на самом деле, когда я заполнил базу данных всего 20 участниками, она работала нормально, но теперь у меня 1168 участников, и каждый раз это время ожидания.Как я могу заставить его работать быстрее.
Models.py
class PartnershipArm(models.Model):
name = models.CharField(max_length = 128)
slug = models.SlugField(unique = True, null=True, blank=True)
partnershipRecords = models.ManyToManyField(Member, through = 'Partnership')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('partnership-arms')
class Partnership(models.Model):
YEAR = []
for r in range((datetime.datetime.now().year), (datetime.datetime.now().year+10)):
YEAR.append((r,r))
MONTHS = (
('January', 'January'),
('February', 'February'),
('March', 'March'),
('April', 'April'),
('May', 'May'),
('June', 'June'),
('July', 'July'),
('August', 'August'),
('September', 'September'),
('October', 'October'),
('November', 'November'),
('December', 'December'),
)
member = models.ForeignKey(Member, on_delete=models.CASCADE)
partnershipArm = models.ForeignKey(PartnershipArm, on_delete=models.CASCADE, db_index = True)
year = models.IntegerField( choices=YEAR, default=datetime.datetime.now().year, db_index = True)
month = models.CharField(max_length = 50, choices = MONTHS, null=True, blank=True, db_index = True)
week1 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week2 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week3 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
week4 = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
total = models.DecimalField(max_digits=10, decimal_places=2, default = 0)
def __str__(self):
return "{0}_{1}_{2}_{3}".format(self.member, self.partnershipArm, self.year, self.month)
def get_absolute_url(self):
return reverse('partnership-arms')
Signals.py
def post_save_partnershipArm_create_receiver(sender, instance=None, created=False,**kwargs):
members = Member.objects.all()
YEARS = []
for r in range((datetime.datetime.now().year), (datetime.datetime.now().year+10)):
YEARS.append((r))
MONTHS = ['January','February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
if created:
for member in members:
for year in YEARS:
for month, counter in enumerate(MONTHS, start = 0):
realMonth = MONTHS[month]
partnership = Partnership(member = member, partnershipArm = instance, year = year, month = realMonth)
partnership.save()