Category : django-queryset

I basically want to do something like this: I want a distinct list of all products from all completed orders, but I want to save/annotate? the order reference(‘refCode’) to the Product object. Order.objects.filter(is_completed=True).order_by(‘-date_created’).products.all().filter(dlLocation__isnull=True).distinct() Notice the ".products.all()". Unfortunately this is no valid code. So my solution was to make 2 seperate querysets like so: completedOrders = ..

Read more

I have two models: class Game(models.Model): id = models.AutoField(primary_key=True) class Score(models.Model): id = models.AutoField(primary_key=True) game = models.ForeignKey(Game, related_name="score", on_delete=models.CASCADE) first_score = models.IntegerField(blank=True) second_score = models.IntegerField(blank=True) is_rusk = models.BooleanField(blank=True) And I got a queryset of Game objects: [ { "id": 314317035, "score": [ { "first_score": 5, "second_score": 1, "is_rusk": false } ] }, { "id": 311298177, ..

Read more

I’m using Python 3.9 and Django 3.2. I have this class, which has a field for another … class Transaction(models.Model): … id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) account = models.ForeignKey(‘Account’, on_delete=models.CASCADE) I woudl like to get the distinct set of Account objects, linked to certain transactions., so I treied this class TransactionManager(models.Manager): def get_accounts_with_pending_transactions(self): """ Returns ..

Read more

On two different PCs I run same code, which contains ‘get_object_or_404’ user = get_object_or_404(User, username = self.kwargs.get(‘username’)) On my PC it runs ok, I get ‘MVidić’ as value. On other PC I get: MVidi%C4%87:1 GET http://app.local/user/MVidi%C4%87 404 (Not Found) Why is that? Is it some localization setting? All Django/Python files are the same on both ..

Read more

Given the models class TaskGroup(models.Model): name = models.CharField(max_length=256) class Task(models.Model): name = models.CharField(max_length=256) group = models.ForeignKey(TaskGroup, on_delete=models.CASCADE) completed = models.BooleanField(default=False) completed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) and the list view class TaskGroupListView(ListView): model = TaskGroup I’d like to display the list of task groups with corresponding tasks. The catch is – I only want to show ..

Read more

I need to limit my update and my delete to Purchases with status "EV", those with status "AP", can only be read. Here is my models: class Purchases(TimeStampedModel): values = models.DecimalField(decimal_places=2, max_digits=10, default=0) cpf = BRCPFField("CPF") status = models.CharField(max_length=2, choices=[(‘AP’, (‘Aprovado’)), (‘EV’, (‘Em validação’))]) modified = datetime.today().strftime(‘%d-%m-%y %H:%M’) def save(self, *args, **kwargs): if self.cpf == ..

Read more

class Item(models.Model): # … required_items = models.ManyToManyField("self", symmetrical=False, blank=True) Since it’s a M2M relation, I can’t use select_related to optimise my query, so I have to use prefetch_related. But I don’t undertstand why I can’t chose the depth of recursion, like I would with select_related and the depth parameter. For example, to select 3 levels ..

Read more