1π
β
If you would like to add a calculated field like all_members
as a part of your model, then you will have to override the save
function:
class Home(models.Model):
...
all_members = models.IntegerField()
def save(self):
all_members = self.all_people()
super(Home, self).save()
Now you can filter by all_members
. It would be better to use the @property
decorator for all_members
, in this case.
Another approach would be to use Djangoβs extra
method as mentioned in a different stackoverflow answer
π€arocks
1π
You still need to define all_members
as a model field (not as an integer), and then populate it with the desired value when you save()
the instance.
class Home(models.Model):
...
number_female = models.IntegerField()
number_male = models.IntegerField()
all_members = models.IntegerField()
def save(self):
self.all_members = self.number_female + self.number_male
super(Home, self).save()
π€desired login
- [Answered ]-Send selection from bootstrap dropdown button with Django
- [Answered ]-Pass database value in form (django)
- [Answered ]-Persist Django user in session
- [Answered ]-How to get a JSON response from a POST form using Django
0π
I think Django Managers can be a solution here. Example:
Custom Manager:
class CustomFilter(models.Manager):
def all_people(self):
return self.number_female + self.number_male
Model:
class Home(models.Model):
....
objects= CustomFilter()
Views:
allpeople= Home.objects.all_people(Home.objects.all())
π€ruddra
Source:stackexchange.com