0

so i have this django model

class terrain(models.Model):
    location=models.CharField(max_length=200)
    size=models.CharField(max_length=200)
    def __str__(self):
        return str(self.location)

how do i get the location of diffrent terrains having the same size ,i used the filter but i have to specify the size for example

data=terrain.objects.filter(size="big")

can't i do this without specifying the size just by pasing the size field

med mansouri
  • 21
  • 1
  • 3
  • [link](https://stackoverflow.com/questions/8989221/django-select-only-rows-with-duplicate-field-values/8989348) this is for you. – Ranu Vijay Sep 20 '21 at 19:11

2 Answers2

0

you need to specify the field. but there are multiple way to filter you can do it on one field or multiple field depends on your requirement.

see documentation: django_ filters

Nandan
  • 404
  • 2
  • 6
  • 15
0
terrains = terrain.objects.all()
data = {}

for terrain in terrains:
    if data.get(terrain.size, None):
        data[terrain.size] = []
    data[terrain.size].append(terrain.location)

print(data) # this will contain all of the locations for different terrain size..
Dharman
  • 30,962
  • 25
  • 85
  • 135
Farid Chowdhury
  • 2,766
  • 1
  • 26
  • 21