0

I have the following Models:

class Categories(models.Model):
    name = models.CharField(max_length=200, unique=True)

class Funnies(models.Model):
    title = models.CharField(max_length=200)
    category = models.ForeignKey(Categories)

In a case where I have a variable holding a category name (myVar), instead of getting all rows in Funnies that hold a reference to the category the long way:

category_id = Categories.objects.get(name = myVar)
funnies_list = Funnies.objects.filter(category = category_id)

Is there a shorter, more "django" way of getting funnies_list ?

Meir

1 Answer 1

3

well if you have myVar already then

funnies_list = Funnies.objects.filter(category__name=myVar) 

would work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.