Django模型过滤器存储在数据库中 [英] Django model filters stored in database
本文介绍了Django模型过滤器存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大致地,它将如下所示:
from news.models import Entry
class NewsContent(models。模型)
filter = models.CharField()
exclude = models.CharField()
offset = models.IntegerField()
limit = models.IntegerField()
#template = models.CharField()
def get_entries(self):
return Entry.objects.filter(self.filter).exclude(self.exclude)[self.offset: self.limit_upper]
这是可能吗?
现在这可能是一个好主意,但这是问题#2
解决方案
你应该能够使用字典进行过滤并排除字段。
想要添加此过滤器:
...过滤器(一个='asdf',两个='xyz')
然后你将存储
{'one':'asdf','two':'xyz'}
作为您的NewsContentModel的过滤器字段中的字符串。
那么你可以这样做
def get_entries(self):
return Entry.objects.filter(** eval(self.filter))
我认为应该工作...
I'm working with content types in feincms. And I want to make a content type that can store filters in the database. Roughly it would look like this:
from news.models import Entry
class NewsContent(models.Model):
filter = models.CharField()
exclude = models.CharField()
offset = models.IntegerField()
limit = models.IntegerField()
#template = models.CharField()
def get_entries(self):
return Entry.objects.filter(self.filter).exclude(self.exclude)[self.offset:self.limit_upper]
Is this possible?
Now this may or may not be a good idea speed wise, but that's question #2
解决方案
You should be able to do that using a dictionary for the filter and exclude fields.
Say you want to add this filter:
...filter(one='asdf', two='xyz')
then you would store
"{'one':'asdf', 'two':'xyz'}"
as a string in your filter field of your NewsContentModel.
then you could do this
def get_entries(self):
return Entry.objects.filter(**eval(self.filter))
I think that should work...
这篇关于Django模型过滤器存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文