开发者

Django model filters stored in database

开发者 https://www.devze.com 2023-01-11 18:28 出处:网络
I\'m working with content types in feincms. And I want to make a content type that can stor开发者_如何学JAVAe filters in the database.

I'm working with content types in feincms. And I want to make a content type that can stor开发者_如何学JAVAe 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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号