Reputation: 157
you need to get the filter
using the get_related_filter
class method
views
modelPath = 'Money.models'
app_model = importlib.import_module(modelPath)
cls = getattr(app_model, 'Money')
related_result = cls().get_related_filter(search_query='search_query')
models.py
class Money(models.Model):
money = models.DecimalField(max_digits=19, blank=True, default=0, decimal_places=2)
def get_related_filter(self, **kwargs):
results = super(Money, self).objects.filter(Q(money__icontains=kwargs['search_query']))
return results
def __str__(self):
return self.money
why gives 'super' object has no attribute 'objects' Python Django
, and does not return filter
Upvotes: 0
Views: 257
Reputation: 476699
It makes no sense to work with super(Money, self)
for two reasons:
Model
, but Model
nor it parents have an objects
attribute; and.objects
on a model class, not the instance.You thus can filter with:
class Money(models.Model):
money = models.DecimalField(max_digits=19, blank=True, default=0, decimal_places=2)
def get_related_filter(self, search_query, **kwargs):
return Money.objects.filter(money__icontains=search_query)
def __str__(self):
return str(self.money)
The __str__
is also supposed to return a string, not a decimal, so you should return str(self.money)
, not self.money
.
Upvotes: 1