how to filter values of main table from foreign key table view?

Viewed 23

I a trying to display the values of a foreign key table sorted upon the values of other foreign table. am unable to find a way to show all the type of categories available in a specific city in the views.

From the 'views' I am able to get all the business's present in the specific city, but along with that I want to find the category types available in the specific city. A help is very much appreciated

#models.py
class business(models.Model):
    title = models.CharField(max_length=200)
    category = models.ForeignKey('Type', on_delete=models.SET_NULL, null=True)
    summary = models.TextField(max_length=1000, help_text="Enter a brief description of the business")
    city = models.ForeignKey('City', help_text="Select a city for this business")


    def get_absolute_url(self):
        return reverse('business-detail', args=[str(self.id)])

    def __str__(self):
        return self.title

class Type(models.Model):
    category_name = models.CharField(max_length=200,help_text="Enter business category")

    def __str__(self):
        return self.category_name

class City(models.Model):
    city_name = models.CharField(max_length=200,help_text="Enter a city")

    def __str__(self):
        return self.city_name
#views.py
class citydetail(generic.ListView):
    model = City
    def get_context_data(self, **kwargs):
        context = super(citydetail, self).get_context_data(**kwargs)
        context['test'] = Type.objects.all()
0 Answers
Related