Answer the question
In order to leave comments, you need to log in
How to customize the sample result of model.objects.filter?
There is such a model:
class Car(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50, blank=False)
discription = models.CharField(max_length=250, blank=True)
date = models.DateTimeField(auto_now_add=True)
location = models.PointField(max_length=40, blank=True,null=True)
active = models.BooleanField(blank=False, default=True)
class Meta:
ordering = ["date"]
def get_nearest_car(request, long, lat):
distance_limit = 0.150
ref_location = Point(float(long), float(lat))
result = Car.objects.filter(location__dwithin=(ref_location, distance_limit))#выбираю записи которые находятся в радиусе 150 от входных координат.
return HttpResponse(serializers.serialize('json', result))
[
{
"model": "app.car",
"pk": 1,
"fields": {
"user": 1,
"name": "1",
"discription": "1",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8697598 37.6647674)"
}
},
{
"model": "app.car",
"pk": 2,
"fields": {
"user": 1,
"name": "2",
"discription": "2",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8694248 37.6647453)"
}
},
{
"model": "app.car",
"pk": 4,
"fields": {
"user": 1,
"name": "4",
"discription": "4",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8691788 37.6650843)"
}
}
]
[
}
"user": 1,
"name": "1",
"discription": "1",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8697598 37.6647674)"
},
{
"user": 1,
"name": "2",
"discription": "2",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8694248 37.6647453)"
},
{
"user": 1,
"name": "4",
"discription": "4",
"date": "2019-05-01T07:08:18.937Z",
"location": "SRID=4326;POINT (55.8691788 37.6650843)"
}
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question