[Answered ]-Django – how to exclude repeating objects

2πŸ‘

βœ…

If you are using postgres, then you can use the distinct queryset filter with a field name.

class BrowseLocationsPageView(ListView):
    def get_queryset(self):
        return self.model.objects.distinct('name')

This solves the β€œissue” but there is a bigger problem at hand. Wherever you are creating the countries, you are creating new countries instead of looking if there is an existing country with the same name – get_or_create may be useful here.

Location should probably have a foreign key to a country too…

πŸ‘€Sayse

0πŸ‘

You have country as a CharField in the Location Model. Hence the repitition.

Change the country field as a ForeignKey in the Location model

Leave a comment