Issue
I have this models.py file in my Django project
from django.db import models
from django.urls import reverse
from django.utils.timezone import now
class Campaign(models.Model):
class Meta:
db_table = 'campaign'
campaign_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, default='')
topic = models.CharField(max_length=255, default='')
sender = models.CharField(max_length=255, default='')
start_date = models.DateTimeField(default=now)
is_active = models.BooleanField(default=False)
draft = models.BooleanField(default=True)
content = models.TextField(default='')
footer = models.CharField(max_length=255, default='')
unsubscribe_message = models.CharField(max_length=255, default='')
@property
def get_completion_percent(self):
return 70
And I'm trying to use the get_completion_percent function in my views.py as a filter right here
def finished_ads_view(request):
queryset = Campaign.objects.filter(get_completion_percent=100)
context = {
'object_list': queryset,
}
return render(request, 'campaigns_in_progress.html', context)
But the error I'm getting is this:
FieldError at /finished_campaigns/ Cannot resolve keyword 'get_completion_percent' into field. Choices are: campaign_id, content, draft, footer, is_active, name, sender, start_date, topic, unsubscribe_message
Can anyone help me make this work?
Solution
You can't filter a property.
If you need a percentage you can use .annotate() and filter the field.
Campaign.objects.annotate(percentage=70).filter(percentage=100)
If you need to make operation on fields of the same record, you can get the values with F() expression
Answered By - Lorenzo Prodon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.