Okay I have a django app with the following conditions:
- Hosted @: PythonAnywhere
- Database: SQLite and MySQL
PROBLEM: At first with few hundreds of data its work okay, but when I reach a few thousands (3k) it takes too long to load it on a simple table.
MY CODE:
models.py
class Outgoing(models.Model):
base_in = models.ForeignKey('warehouse.Incoming', related_name='out', on_delete = models.SET_NULL, null=True)
trans_date = models.DateField('Date', default=timezone.now)
trans_type = models.CharField('Type', max_length=50, choices = OUTGOING_TYPE)
form_no = models.CharField('FORM No', max_length=20, default=0)
project_site = models.ForeignKey(ProjectSite, related_name='out_project_site', null=True, on_delete = models.SET_NULL)
released_by = models.ForeignKey(User, related_name='out_released_by', default='', on_delete = models.SET_NULL, null=True)
released_to = models.ForeignKey(User, related_name='out_released_to', blank=True, null=True, on_delete = models.SET_NULL)
released_out = models.ForeignKey(Outsider, related_name='outsider_released_to', blank=True, null=True, on_delete = models.SET_NULL)
unit = models.ForeignKey(UnitProfile, related_name='user_unit', blank=True, null=True, on_delete = models.SET_NULL)
quantity = models.DecimalField('Quantity', db_index=True, max_digits=20, decimal_places=2, default=0)
details = models.CharField('Details', max_length=200, default='')
attachment = models.FileField('Form', upload_to='incoming_form', blank=True)
create_date = models.DateTimeField('Date Created', auto_now_add=True)
def __str__(self):
return "%s" %(self.trans_date)
class Meta:
verbose_name = 'Outgoing'
verbose_name_plural = 'Outgoings'
views.py
class OutgoingView(ListView):
model = Outgoing
template_name = 'warehouse/outgoing_page.html'
context_object_name = 'all_out'
outgoing_page.html
<tbody>
{% for outgoing in all_out%}
<tr>
<td class="text-truncate">{{ outgoing.trans_date }}</td>
<td class="text-truncate">{{ outgoing.trans_type }}</td>
<td class="text-truncate">{{ outgoing.form_no }}</td>
<td class="text-truncate info">{{ outgoing.base_in.item }}</td>
<td class="text-truncate danger">{{ outgoing.quantity|intcomma }}</td>
<td class="text-truncate">{{ outgoing.project_site }}</td>
<td class="text-truncate">{{ outgoing.unit }}</td>
<td class="text-truncate">{{ outgoing.released_by }}</td>
<td class="text-truncate">{{ outgoing.released_to }}</td>
<td class="text-truncate">{{ outgoing.released_out }}</td>
<td class="text-truncate">{{ outgoing.details }}</td>
<td class="text-truncate">
<i class="la la-pencil font-medium-3"></i>
</td>
</tr>
{% endfor %}
</tbody>
What I did: - simplified my views the best I can. Did not even do any simple sorting as I've read that hits the database again. - Shift from SQLite to MySQL - Asked help from the guys at pythonanywhere thinking I might not have enough workers, they said my account is good.
What puzzles me is that even when I try to load them via admin page, its still very slow.
Any help would be great, thanks.