Improving performance of an Django MPTT tree - django

I have implemented the follow model to capture the structure of a classical piece of music. I'm using the MPTT to implement movements, opera acts and arias.
model.py:
TreeForeignKey(Work, blank=True, null=True, db_index=True, on_delete=models.PROTECT).contribute_to_class(Work, 'parent')
mptt.register(Work, order_insertion_by=['id'])
class Work(models.Model):
attributed_to = models.NullBooleanField()
name = models.CharField(max_length=400, null=True, blank=True)
lang = models.CharField(max_length=2, null=True, blank=True)
name_original = models.CharField(max_length=200, null=True, blank=True)
lang_original = models.CharField(max_length=2, null=True, blank=True)
name_common = models.CharField(max_length=200, null=True, blank=True)
name_common_orig = models.CharField(max_length=200, null=True, blank=True)
dedicated_to = models.CharField(max_length=200, null=True, blank=True)
pic = models.ImageField(upload_to = 'pic_folder/', default = '/pic_folder/None/no-img.jpg')
piece_type = models.CharField(max_length=100, null=True, blank=True)
category = models.CharField(max_length=100, null=True, blank=True)
date_start = models.CharField(max_length=100, null=True, blank=True)
date_start_gran = models.CharField(max_length=5, choices=DATE_TYPES, default='Year')
date_signature = models.CharField(max_length=100, null=True, blank=True)
date_signature_gran = models.CharField(max_length=10, choices=DATE_TYPES, default='Year')
around = models.BooleanField(null=True)
date_published = models.CharField(max_length=100, null=True, blank=True)
date_published_gran = models.CharField(max_length=10, choices=DATE_TYPES, default='Year')
desc = models.TextField(max_length=8000, null=True, blank=True)
order = models.CharField(max_length=100, null=True, blank=True)
class Work_Music(Work):
composer = models.ForeignKey(Composer, verbose_name=_('composer'), null=True, blank=True, on_delete=models.PROTECT)
key = models.CharField(max_length=10, null=True, blank=True)
tonality = models.CharField(max_length=20, null=True, blank=True)
Here is the view.py:
class ComposerOverviewView(TemplateView):
template_name = 'composers/overview/catalogue.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
opus = Work_Music.objects.filter(composer=self.kwargs['pk'], level=0)
context.update({
'composer': Composer.objects.get(pk=self.kwargs['pk']),
'opus': opus,
})
return context
When I try to run a query for all of the works for a composer (in the DB there are a total of 264 works), the queryset take almost 6 secs to run. I was wondering if anyone knows how to improve my code to increase the performance. There seems to be some with the delay_mptt_updates() method. But I'm not sure where it would go in my code.
I read in another article that MPTT sometimes does run slow. If I can increase the performance, what are some other alternatives?

Related

It is not possible to migrate a new user element to the django table

I am faced with the problem that it is not possible to migrate the new user value to the database table. At first there were errors on related_name, but I fixed it, and now that this value cannot be zero, at the same time, if I write that null=True, then the user cannot be displayed in the records in the future.
class Writehelp(models.Model):
users = models.ForeignKey(User,on_delete = models.CASCADE,related_name='helpedman',null=False,verbose_name='Автор')
titles = models.CharField(max_length=200, verbose_name='Заголовок', blank=True)
descriptions = models.TextField(blank=True, verbose_name='Описание')
createdtimes = models.DateField(auto_now_add=True, db_index=True, verbose_name='Дата создания')
prices = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='Цена')
course = models.IntegerField(null=True, blank=True, verbose_name='Курс')
semestr = models.IntegerField(null=True, blank=True, verbose_name='Семестр')
subjects = models.CharField(max_length=200, null=True, blank=True, verbose_name='Предмет')
institutes = models.CharField(max_length=200, null=True, blank=True, verbose_name='Институт')
However, when I migrated this model, there were no problems:
class UploadFile(models.Model):
user = models.ForeignKey(User,on_delete = models.CASCADE,related_name='file_created' ,verbose_name='Автор')
title = models.CharField(max_length=200, verbose_name='Заголовок',blank=True)
# uploadedfile = models.FileField(upload_to='files/',null=True, verbose_name='Файл')
description = models.TextField(blank=True, verbose_name='Описание')
createdtime = models.DateField(auto_now_add=True, db_index=True, verbose_name='Дата создания')
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='Цена')
number_course = models.IntegerField(null=True, blank=True, verbose_name='Курс')
number_semestr = models.IntegerField(null=True, blank=True, verbose_name='Семестр')
subjectt = models.CharField(max_length=200, null=True,blank=True,verbose_name='Предмет')
type_materials = models.CharField(max_length=200,null=True,blank=True, verbose_name='Тип работы')
institute = models.CharField(max_length=200, null=True,blank=True, verbose_name='Институт')
Add defult=None to uploadedfile field in your UploadFile model.

Cannot resolve keyword annotated field django

this is my model
class ExchangeReportTime(models.Model):
creator = models.ForeignKey('accounts.Account', on_delete=models.CASCADE, null=True, verbose_name=_('Creator'))
create_time = models.DateTimeField(default=timezone.now)
exchange = models.ForeignKey(BorseExchange, on_delete=models.SET_NULL, null=True,
related_name='report_times')
actual_fiscal_month = models.CharField(max_length=255, null=True, blank=True)
fiscal_month = models.CharField(max_length=5, null=True, blank=True)
fiscal_year = models.CharField(max_length=255, null=True, blank=True)
is_fiscal_year_changed = models.BooleanField(null=True, blank=True)
period_ending_date = models.CharField(max_length=15, null=True, blank=True)
statement_key = models.IntegerField(null=True, blank=True)
statement_type = models.CharField(max_length=50, null=True, blank=True)
...
fiscal_month and fiscal_year are string so i change types and query as below
exchange = BorseExchange.objects.get(tse_code=exchange_number)
last_12_season = exchange.report_times.filter(
statement_type='InterimIncomeStatement',
period_type='seasonally'
).annotate(
fiscal_month_number=Cast('fiscal_month',IntegerField()),
fiscal_year_number=Cast('fiscal_year',IntegerField()),
).order_by(
'-fiscal_year_number', '-fiscal_month_number'
).distinct(
'fiscal_month_number', 'fiscal_year_number'
)[:records_count]
print(last_12_season.values_list('id'))
but i give error:
django.core.exceptions.FieldError: Cannot resolve keyword 'fiscal_month_number' into field. Choices are: actual_fiscal_month, attachment_url, auditing, company_key, create_time, creator, creator_id, currency, exchange, exchange_id, fiscal_month, fiscal_year, html_url, id, is_empty, is_fiscal_year_changed, period_ending_date, period_type, publish_date, publish_time, report_items, scenario, statement_key, statement_type
where is the problem?
python3.6 and django 2.2.*

Creating a Django model ForeignKey field from a specific queryset?

Ok, this is a hard one to explain.
I Have these models:
class Supplier(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
class Product(models.Model):
description = models.CharField(max_length=30)
costprice = models.FloatField(null=True, max_length=99, blank=True)
retailprice = models.FloatField(null=True, max_length=99, blank=True)
barcode = models.CharField(null=True, max_length=99, unique=True, blank=True)
image = DefaultStaticImageField(null=True, blank=True,default='images/item_gC0XXrx.png')
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, null=True, blank=True)
class Order(models.Model):
item = models.ForeignKey(Product, on_delete=models.CASCADE, default='')
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
item_qty = models.IntegerField(null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
My ultimate goal is to be able to create an 'Order' pulling items from a specific supplier.
Now, I have no idea as to make that happen - how do I create an instance of an 'Order',in a form of some sort, that will fetch items from the selected supplier and not all items from everybody else?
You are looking for limit_choices_to

FOREIGN KEY constraint failed in signals

I tried to insert data into the table Account, but I keep getting this error message.
I delete all my migrations and make migrations again but I keep getting this error.
Here is my models.
class Student(models.Model):
user = models.OneToOneField(Account, on_delete=models.CASCADE)
name = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=15, blank=True, null=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True, null=True)
section = models.ForeignKey(Section, on_delete=models.CASCADE, default=1)
dept = models.ForeignKey(Dept, on_delete=models.CASCADE, blank=True, null=True)
batch = models.CharField(max_length=15, null=True, blank=True)
USN = models.CharField(primary_key='True', max_length=100)
DOB = models.DateField(default='1998-01-01')
profile_image = models.ImageField(upload_to='student/profile_images/%Y/%m/%d/', blank=True, null=True)
address = models.CharField(max_length=300, null=True, blank=True)
nationality = models.CharField(max_length=100, null=True, blank=True)
guardian_name = models.CharField(max_length=200, null=True, blank=True)
guardian_number = models.CharField(max_length=15, null=True, blank=True)
guardian_address = models.CharField(max_length=300, null=True, blank=True)
blood_group = models.CharField(max_length=3, null=True, blank=True)
exam_name = models.CharField(max_length=300, null=True, blank=True)
background = models.CharField(max_length=300, null=True, blank=True)
passing_year = models.CharField(max_length=8, null=True, blank=True)
score = models.CharField(max_length=10, null=True, blank=True)
school_name = models.CharField(max_length=300, null=True, blank=True)
country = models.CharField(max_length=100, null=True, blank=True)
certificate = models.FileField(upload_to='certificates/%Y/%m/%d/', null=True, blank=True)
Here is Account model.
class Account(AbstractBaseUser, PermissionsMixin):
unid = models.CharField(max_length=20, unique=True, null=True, blank=True)
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True)
user_permissions = models.ManyToManyField(
Permission,
verbose_name=_('user permissions'),
blank=True,
null=True,
help_text=_('Specific permissions for this user.'),
related_name="user_set",
related_query_name="user",
)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
registered = models.BooleanField(default=False)
so what I did is that when I create account if the is_student is True I create a signals that going to create student model for that account instance automatically.
here is the signals
def create_student_or_teacher(sender, created, instance, **kwargs):
if created:
if instance.is_student:
Student.objects.create(
user=instance,
USN=instance.unid,
)
elif instance.is_teacher:
Teacher.objects.create(
user=instance,
id=instance.unid,
)
So when I create the account for student the tracker will highlight in the signals Student.objects.create(...)
If you have any idea I hope it would help me.
UPDATED:
When I'm creating teacher it is working find.
here is the teacher model
class Teacher(models.Model):
name = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=15, blank=True, null=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
salary = models.IntegerField(default=1)
school = models.ForeignKey(School, blank=True, null=True, on_delete=models.CASCADE)
section = models.ForeignKey(Section, blank=True, null=True, on_delete=models.CASCADE)
user = models.OneToOneField(Account, on_delete=models.CASCADE, null=True)
id = models.CharField(primary_key=True, max_length=100)
dept = models.ForeignKey(Dept, on_delete=models.CASCADE, blank=True, null=True)
DOB = models.DateField(default='1980-01-01')
profile_image = models.ImageField(upload_to='teacher/profile_images/%Y/%m/%d/', blank=True, null=True)
nationality = models.CharField(max_length=300, null=True, blank=True)
address = models.CharField(max_length=300, null=True, blank=True)
Finally after few hours I solved my error it was simple
in the section I set default value which was not exist
so I just change the value to an exist one and that was it.

Can`t manually delete record from sqlite table, because related table with __old does not exists

I want to manually delete records from sqlite table(witch was created with Django 1.10, Table from i want to delete records called main.storage_claim, it has relation to main.storage_claimlist), but got error: Error deleting record: no such table: main.storage_claimlist__old.
I dont know why it want this table.
I have only main.storage_claimlist without __old.
Btw, i`m using DB Browser for SQLite as gui tool for working with sqlite.
Can someone explain what the heck is __old?
ClaimList model:
class ClaimList(models.Model):
""" Claim list contains one to many claim. Used for grouping claims. """
created_at = models.DateTimeField(default=datetime.now, blank=True, null=True)
from_storage = models.ForeignKey(
Storage, related_name='from_storage', blank=True, null=True, default=True)
to_storage = models.ForeignKey(
Storage, related_name='to_storage', blank=True, null=True, default=True)
status = models.CharField(max_length=255, blank=True, null=True)
separation = models.CharField(max_length=255, blank=True, null=True)
insurance_company = models.ForeignKey(
InsuranceCompany, null=True, blank=True)
patient = models.CharField(max_length=255, blank=True, null=False)
Claim model:
class Claim(models.Model):
""" Claim contain information about what need to be transefer """
medicine = models.CharField(max_length=255, blank=True, null=True)
claim_list = models.ForeignKey(ClaimList, blank=True, null=True)
requested_amount = models.DecimalField(
default=0, max_digits=5, decimal_places=0)
given_amount = models.DecimalField(
default=0, max_digits=5, decimal_places=0)
requested_unit = models.CharField(max_length=100, blank=True, null=True)
given_unit = models.CharField(max_length=100, blank=True, null=True)
from_income = models.ForeignKey(Income, blank=True, null=True)
status = models.CharField(max_length=100, blank=True, null=True)
is_will_be_accepted_in_future = models.BooleanField(default=False)
price = models.DecimalField(null=True, max_digits=10, decimal_places=5)
sum = models.DecimalField(null=True, max_digits=10, decimal_places=5)
objects = ClaimManager()