views:

44

answers:

1

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

A: 

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...

Matthew J Morrison
There is still the security issue with eval. But you're saying this: ...filter({one:'asdf', two:'xyz'})would actually work?
Arnar Yngvason
filter({'one':'asdf', 'two':'xyz'}) would not work, but filter(**{'one':'asdf', 'two':'xyz'}) would. Anytime you "eval" anything you don't trust, there is a risk...
Matthew J Morrison
...also, you may be able to use pickle to serialize/deserialize your filter dictionary if you want to avoid using eval.
Matthew J Morrison
You should probably use a proper JSON encoder/decoder for that. It isn't too hard to build a Form class able to edit JSON data through automatically generated form fields either.
Matthias Kestenholz