django-orm

Django ORM & Django REST Framework: How to make "grouped" arrays?

Django ORM & Django REST Framework: How to make "grouped" arrays? Question: Currently, my Django server can return the JSON below: [ { "id": 1, "customer": { "id": 1, "name": "John Doe" }, "description1": "…", "description2": "…", "description3": "…", "description4": "…" }, { "id": 2, "customer": { "id": 1, "name": "John Doe" }, "description1": "…", …

Total answers: 2

List comprehension in django orm

List comprehension in django orm Question: I have Gallery model: class Gallery(models.Model): TERMS_CHOICES = [ (1, ‘1 month’), (3, ‘3 months’), (6, ‘6 months’), (12, ’12 months’), ] date = models.DateField(default=datetime.now().strftime(‘%Y-%m-%d’), editable=False) term = models.IntegerField(choices=TERMS_CHOICES, default=1) I’m trying to create a scheduler function: it have to check expired terms, and delete the gallery. If term …

Total answers: 1

Filter returns much more objects

Filter returns much more objects Question: I have two models: class PhotoAlbum(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, auto_created=True) name = models.CharField(max_length=50, verbose_name=’Album name’) class AlbumImage(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, auto_created=True) album = models.ForeignKey(PhotoAlbum, on_delete=models.CASCADE, related_name=’photos’) image = models.ImageField(upload_to=’images/’, height_field=None, width_field=None, max_length=100, blank=True) When i send GET request without query parameters i see a normal …

Total answers: 1

Django ORM JOIN of models that are related through JSONField

Django ORM JOIN of models that are related through JSONField Question: If I have 2 models related through ForeignKey I can easily get a queryset with both models joined using select_related class Foo(Model): data = IntegerField() bar = ForeignKey(‘Bar’, on_delete=CASCADE) class Bar(Model): data = IntegerField() foos_with_joined_bar = Foo.objects.select_related(‘bar’) for foo in foos_with_joined_bar: print(foo.data, foo.bar.data) # …

Total answers: 3

Django order model by date and move models without date to the end

Django order model by date and move models without date to the end Question: I have a model tickets, with a sum in it. I’m trying to make spending tickets by nearest expire date. But if I’m ordering by date_expire, it move date_expire=None models to the top. But date_expire=None means that ticket has no limit …

Total answers: 2

Django annotate field value from external dictionary

Django annotate field value from external dictionary Question: Lets say I have a following dict: schools_dict = { ‘1’: {‘points’: 10}, ‘2’: {‘points’: 14}, ‘3’: {‘points’: 5}, } And how can I put these values into my queryset using annotate? I would like to do smth like this, but its not working schools = SchoolsExam.objects.all() …

Total answers: 1

How can I write this sql query in django orm?

How can I write this sql query in django orm? Question: I have a sql query that works like this, but I couldn’t figure out how to write this query in django. Can you help me ? select datetime, array_to_json(array_agg(json_build_object(parameter, raw))) as parameters from dbp_istasyondata group by 1 order by 1; Asked By: Fırat Badur …

Total answers: 1

How to get reference table fields with django model query

How to get reference table fields with django model query Question: When I am trying to fetch foreign key table using django model I am only unable to get the referenced table details. I have two models TblVersion and TblProject defined below class TblVersion(models.Model): version_id = models.AutoField(primary_key=True) project = models.ForeignKey(TblProject, models.DO_NOTHING) version_major = models.PositiveSmallIntegerField() version_minor …

Total answers: 2

Find queryset where one field is greater than another one

Find queryset where one field is greater than another one Question: I have model Shop, I want to get all shops which time_open(opening time) is more than time_closed(closing time). For example 23:00:00 is more than 07:00:00 (Shop_2(Night shift shop)). class Shop(models.Model): time_open = models.TimeField() time_closed = models.TimeField() for example i have two objects: shop_1 = …

Total answers: 1

How to cache SerializerMethodField result

How to cache SerializerMethodField result Question: I have the below serializer: class OrderItemResponseSerializer(serializers.ModelSerializer): prepack_qty = serializers.SerializerMethodField() product_description = serializers.SerializerMethodField() class Meta: model = OrderItem fields = ( "product", "qty_ordered", "qty_approved", "status", "prepack_qty", "product_description" ) def get_prepack_qty(self, obj): return obj.product.prepack_quantity def get_product_description(self, obj): return obj.product.product_description When I make a get request to /orders, I make a …

Total answers: 2