我正在使用django + tastypie + postgreSQL来处理Web应用程序的后端。然后,在另一个“项目”上,我的朋友正在实现一个前端,它将利用我的后端API来创建AJAX调用,从而用一些数据填充html。好。到目前为止,我知道该怎么做。
为Restful API创建不同的资源时出现问题。我有,我的模型定义如下:
# COUNTRY + MANAGER
class CountryManager(models.Manager):
def create(**kwargs):
try:
country = Country.objects.get(short_name=kwargs['short_name'])
except:
country = Country(name=kwargs['name'], short_name=kwargs['short_name'])
country.save()
return country
class Country(models.Model):
short_name = models.CharField(max_length=max_short_len, unique=True)
name = models.CharField(max_length=max_short_len, unique=False)
lat = models.DecimalField(max_digits=11, decimal_places=9, default=0.0)
lon = models.DecimalField(max_digits=12, decimal_places=9, default=0.0)
objects = CountryManager()
# REGION +`MANAGER
class RegionManager(models.Manager):
def create(**kwargs):
try:
region = Region.objects.get(short_name=kwargs['short_name'])
except:
region = Region(name=kwargs['name'], short_name=kwargs['short_name'], country=kwargs['country'])
region.save()
return region
class Region(models.Model):
name = models.CharField(max_length=max_short_len, unique=False, default='NoName')
short_name = models.CharField(max_length=max_short_len, unique=False, default='NoName')
lat = models.DecimalField(max_digits=11, decimal_places=9, default=0.0)
lon = models.DecimalField(max_digits=12, decimal_places=9, default=0.0)
country = models.ForeignKey('Country')
objects = RegionManager()
# CITY + MANAGER
class CityManager(models.Manager):
def create(**kwargs):
try:
city = City.objects.get(short_name=kwargs['short_name'])
city.lat = kwargs['lat']
city.lon = kwargs['lon']
except:
city = City(name=kwargs['name'], short_name=kwargs['short_name'], lat=kwargs['lat'], lon=kwargs['lon'])
city.save()
return city
class City(models.Model):
name = models.CharField(max_length=max_short_len, unique=False)
short_name = models.CharField(max_length=max_short_len, unique=False, default='NoName')
lat = models.DecimalField(max_digits=11, decimal_places=9, default=0.0)
lon = models.DecimalField(max_digits=12, decimal_places=9, default=0.0)
region = models.ForeignKey('Region')
objects = CityManager()
现在,如果可能的话,我希望将我/位置资源映射为城市,地区和国家的混合。因此,如果我GET / api / location / 1,我会获得一个类似的JSON:
{"location":[
{"city":"London",
"region":"London",
"country":"UK"
}]
}
另外,如果我POST / api / location / 1,提供JSON我想在我的模型文件中调用City类中的方法:
def saveLocation(**kwargs):
# countryN, countrySN, regionN='No region', regionSN='No region', cityN, citySN, cityLat, cityLon, locationType
# define args
countryN = kwargs.get('countryN', None)
countrySN = kwargs.get('countrySN', None)
regionN = kwargs.get('regionN', None)
regionSN = kwargs.get('regionSN', None)
cityN = kwargs.get('cityN', None)
citySN = kwargs.get('citySN', None)
cityLat = kwargs.get('cityLat', None)
cityLon = kwargs.get('cityLon', None)
locationType = kwargs.get('locationType', None)
# put nulls in the args
if regionN is None: regionN = 'No region'
if regionSN is None: regionSN = 'No region'
# control over the params
if regionSN is None or countrySN is None or citySN is None: raise Exception('Invalid parameters')
#Save the country
country = Country.objects.create(name=countryN, short_name=countrSN)
countryId = country.pk
#Save the region, if any. Else save it like "no region"
region = Region.objects.create(name=regionN, short_name=regionSN, country = country)
regionId = region.pk
#Save the city
city = City.objects.create(name=cityN, short_name=citysN, lat=cityLat, lon=cityLon, region=regionId)
return city
我遵循了Tastypie手册(http://django-tastypie.readthedocs.org/en/latest/cookbook.html),但它只适用于资源=模型环境。
此外,我尝试实现自己的类MyResource(Resource)以避免ModelsResource所做的直接模型映射。但我没有这方面的技能或知识。
我想请求一些git repo或一些示例代码,在那里我可以学习如何使用Django实现Restful API而无需使用resource = model。
提前致谢。
答案 0 :(得分:0)
我认为你应该创建一个Location模型,或者使用City模型作为基础。想想看,“真实”的位置是城市,地区和国家都是继承的。
我对此的建议是创建一个简单的资源(不是模型资源)并实现您需要的方法:
obj_create
(创建):https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py#L1018
obj_get_list
用于获取位置列表(GET /位置/):https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py#L967
obj_get
用于获取单个位置(/ location / 1 /):https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py#L992
这应该涵盖你所需要的一切。看一下ModelResource
,了解这些方法是如何实现的。