开发者

Django - storing logical tests as records

I'm working on a Gran Turismo 5 Django application. Here's a very simplified data model:

class Event(models.Model):
    name = models.CharField(max_length=256, unique=True)

class EventCriteria(models.Model):
    name = models.CharField(max_length=256, unique=True)
    events = models.ManyToManyField(Event)
    test = ???

class Country(models.Model):
    name = models.CharField(max_length=256, unique=True)

class Make(models.Model):
    name = models.CharField(max_length=256, unique=True)
    country = models.ForeignKey(Country)

class Car(models.Model):
    name = models.CharField(max_length=256, unique=True)
    make = models.ForeignKey(Make)

class Setup(models.Model):
    name = models.CharField(max_length=256, uni开发者_JAVA技巧que=True)
    car = models.ForeignKey(Car)
    horsepower = models.IntegerField()

For example, a given event might have the criteria 'Country = Italy'. When applied against the model above, that would require a test like the following:

setup.car.make.country.name == u'Italy'

Does anyone have a good framework for how I might structure the EventCriteria model (especially the 'test' field or fields') to make a) storing these tests and b) applying them as filters in future views possible?

Thanks,

Mike


It's not clear on why your "test" isn't a simple boolean field. The question is confusing. I'm assuming that really want a persistent filter, since that's often requested.

A Django filter is a dictionary.

SomeModel.objects.filter( column=value, column__contains=value )

SomeModel.objects.filter( **{'column':value, 'column__contains':value} )

You can do this to persist your "test".

  1. Convert your "filter" expression to a dictionary.

  2. JSON-encode the dictionary as a BLOB

  3. Save it.

You can apply your test as follows.

  1. Get the filter BLOB

  2. JSON-decode the dictionary

  3. Use the dictionary in a filter for the appropriate class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜