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 ..
Category : django-orm
I’m trying to do a query where I use an average of a column against its latest value and then use it in Case and When. I can successfully annotate the monthly average and latest field but not compare them in the DB: this is the direction of what I’m trying to do latest_known_kpi_obj = ..
Users in my postgresql database have field that stores phone numbers in different formats, like: 34567889 +34567889 +(345)67889 or even +(345) 678 89 Having the string with only digits (34567889) I want to be able to find a record no matter of format. I am trying to use regex search for this (with no luck), ..
How to retrieve a substring after a delimiter from sql in Django ? For example, I have "2021-22:32" saved in my sql database. I wish to run a query which returns "32", i.e. substring after the delimiter ‘:’. How can I do that in Django ? Say my model is class model(models.Model): str=models.CharField(max_length=25) Suggest a ..

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 ..
I am using the Django ORM and I would like to do something like: self.queryset.annotate(cupcake_name=Q(baked_goods__frosted_goods__cupcakes__cupcake_id=’xxx’)) but return the individual cupcake name field value somehow so I can serve it as a data attribute. Source: Python..
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: ..
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’)) ..
I have following table. Salesman salesman_id | name | city | commission ————-+————+———-+———— 5001 | James Hoog | New York | 0.15 5002 | Nail Knite | Paris | 0.13 5005 | Pit Alex | London | 0.11 5006 | Mc Lyon | Paris | 0.14 5007 | Paul Adam | Rome | 0.13 5003 ..

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) ..
Recent Comments