我找到了一个解决方案,我不知道它是否是最好的解决方案,但它对我有用,我想分享它。如果有人找到更好的方法来做到这一点,欢迎您提出更改建议。
我的模特:
""" The following 3 models deal with tables that already exist in the database """
class Countries(models.Model):
name = models.CharField(max_length=100)
iso3 = models.CharField(max_length=3, blank=True, null=True)
iso2 = models.CharField(max_length=2, blank=True, null=True)
phonecode = models.CharField(max_length=255, blank=True, null=True)
capital = models.CharField(max_length=255, blank=True, null=True)
currency = models.CharField(max_length=255, blank=True, null=True)
currency_symbol = models.CharField(max_length=255, blank=True, null=True)
tld = models.CharField(max_length=255, blank=True, null=True)
native = models.CharField(max_length=255, blank=True, null=True)
region = models.CharField(max_length=255, blank=True, null=True)
subregion = models.CharField(max_length=255, blank=True, null=True)
timezones = models.TextField(blank=True, null=True)
translations = models.TextField(blank=True, null=True)
latitude = models.DecimalField(max_digits=10, decimal_places=8, blank=True, null=True)
longitude = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
emoji = models.CharField(max_length=191, blank=True, null=True)
emojiu = models.CharField(db_column='emojiU', max_length=191, blank=True, null=True) # Field name made lowercase.
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField()
flag = models.IntegerField()
wikidataid = models.CharField(db_column='wikiDataId', max_length=255, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'countries'
class States(models.Model):
name = models.CharField(max_length=255)
country = models.ForeignKey(Countries, models.DO_NOTHING)
country_code = models.CharField(max_length=2)
fips_code = models.CharField(max_length=255, blank=True, null=True)
iso2 = models.CharField(max_length=255, blank=True, null=True)
latitude = models.DecimalField(max_digits=10, decimal_places=8, blank=True, null=True)
longitude = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField()
flag = models.IntegerField()
wikidataid = models.CharField(db_column='wikiDataId', max_length=255, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'states'
class Cities(models.Model):
name = models.CharField(max_length=255)
state = models.ForeignKey('States', models.DO_NOTHING)
state_code = models.CharField(max_length=255)
country = models.ForeignKey('Countries', models.DO_NOTHING)
country_code = models.CharField(max_length=2)
latitude = models.DecimalField(max_digits=10, decimal_places=8)
longitude = models.DecimalField(max_digits=11, decimal_places=8)
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
flag = models.IntegerField()
wikidataid = models.CharField(db_column='wikiDataId', max_length=255, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'cities'
当然,数据库来自这个项目,如果有人需要的话
https://dr5hn.github.io/countries-states-cities-database/
这是我的观点
class CityViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = CitiesSerializer
def get_queryset(self):
city = self.request.query_params.get("cityname")
queryset = Cities.objects.filter(name__startswith=city)
return queryset
最后是我的 serializers.py
""" Fields:
'name','country','country-code','fips_code','iso2','latitude','longitude','created_at','updated_at','flag','wikidataid'
"""
class StatesSerializer(serializers.ModelSerializer):
class Meta:
model = States
fields = ['name', 'iso2']
""" Fields
'name','iso3','iso2','phonecode','capital','currency_symbol','tld','native','region','subregion','timezones','translations','latitude','longitude','emoji','emojiu','created_at','updated_at','flag','wikidataid'
"""
class CountriesSerializer(serializers.ModelSerializer):
class Meta:
model = Countries
fields = ['name', 'iso2', ]#'translations']
"""Fields:
'name','state','state_code','country','country_code','latitude','longitude','created_at','updated_at','flag','wikidataid'
"""
class CitiesSerializer(serializers.ModelSerializer):
state = StatesSerializer(many=False)
country = CountriesSerializer(many=False)
class Meta:
model = Cities
fields = ['name', 'state', 'country', 'latitude', 'longitude']
使用此代码,我将获得正确的预期 JSon 输出。当然,它必须由适当的 forntend 来阅读,例如 JQuery、Vue 甚至 Vanilla JavaScript,但问题的对象是完整的。
如果你知道更好的方法,请不要犹豫。