Category : django-orm

Relevant models are: class Score(models.Model): chouette = models.ForeignKey( Chouette, on_delete=models.CASCADE, related_name="scores" ) game = models.IntegerField(blank=True, null=True) player = models.ForeignKey(User, on_delete=models.CASCADE, related_name="scores") position = models.CharField(max_length=10, blank=True, null=True) score = models.DecimalField(max_digits=10, decimal_places=3, blank=True, null=True) objects = ScoreManager() class User(AbstractUser): name = CharField(_("Name of User"), blank=True, max_length=255) handle = CharField(max_length=10) xp = IntegerField(default=0) objects = CustomUserManager() I have ..

Read more

Recently, I watched Django and discovered the teaching videos of select_related and prefetch_related. So I installed debug_toolbar and took a look at my website I searched the database too many times on one page. I must convert it to json and send it back to the front end Can I still optimize? Is this the ..

Read more

How can I change the output of the models.ForeignKey field in my below custom field? Custom field: class BetterForeignKey(models.ForeignKey): def to_python(self, value): print(‘to_python’, value) return { ‘id’: value.id, ‘name_fa’: value.name_fa, ‘name_en’: value.name_en, } def get_db_prep_value(self, value, connection, prepared=False): print(‘get_db_prep_value’) return super().get_db_prep_value(value, connection, prepared) def get_prep_value(self, value): print(‘get_prep_value’) return super().get_prep_value(value) And used in the below model: ..

Read more

I have query like which is given below todayData = User.objects.filter(log__created_at__date=curDate,og_type=’credit’).annotate( max_price=Sum(‘log__bits’)).exclude(log__bits=300).order_by(‘-max_price’) through this query i am getting the user which have max count of credit amount after this through this query i am getting the total count of match and user total price newList = [] for user in todayData: highCredit = Log.objects.filter(user_id=user.id,log_type=’credit’,created_at__date=curDate).exclude( bits=300).aggregate(credit=Sum(‘price’),match_count=Count(‘match’)) ..

Read more

I am trying to enable the user to be able to add job and after filling the form i got that error. Here is my code: models.py from django.db import models from django.contrib.auth.models import User class Job(models.Model): title = models.CharField(max_length=255) short_description = models.TextField() long_description = models.TextField(blank=True, null=True) created_by = models.ForeignKey(User, related_name=’jobs’, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) ..

Read more