I have a model containing ranges of IP addresses similar to this:
class Country(db.Model):
begin_ipnum = db.IntegerProperty()
end_ipnum = db.IntegerProperty()
In the SQL database, I could find rows containing IP in a specific range, for example:
SELECT * FROM Country WHERE ipnum BETWEEN begin_ipnum AND end_ipnum
or that:
SELECT * FROM Country WHERE begin_ipnum < ipnum AND end_ipnum > ipnum
Unfortunately, GQL allows only inequality filters by one property and does not support syntax BETWEEN. How can I get around this and build a query equivalent for them in App Engine?
Also, can it ListPropertybe "live" or should it be computed when creating the record?
question updated with first strike to resolve:
So, based on David's answer below and articles such as:
http://appengine-cookbook.appspot.com/recipe/custom-model-properties-are-cute/
I am trying to add a custom field to my model as follows:
class IpRangeProperty(db.Property):
def __init__(self, begin=None, end=None, **kwargs):
if not isinstance(begin, db.IntegerProperty) or not isinstance(end, db.IntegerProperty):
raise TypeError('Begin and End must be Integers.')
self.begin = begin
self.end = end
super(IpRangeProperty, self).__init__(self.begin, self.end, **kwargs)
def get_value_for_datastore(self, model_instance):
begin = self.begin.get_value_for_datastore(model_instance)
end = self.end.get_value_for_datastore(model_instance)
if begin is not None and end is not None:
return range(begin, end)
class Country(db.Model):
begin_ipnum = db.IntegerProperty()
end_ipnum = db.IntegerProperty()
ip_range = IpRangeProperty(begin=begin_ipnum, end=end_ipnum)
, , ListProperty, :
q = Country.gql('WHERE ip_range = :1', my_num_ipaddress)
, , :
...
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 619, in _attr_name
return '_' + self.name
TypeError: cannot concatenate 'str' and 'IntegerProperty' objects
attr_name self.name, , , . ?