Question

I'm new to Django, sorry if this is a basic question.

I need to list the values of "n_category" in a Directory.

My model is:

from django.db import models
import datetime

class Category(models.Model):
    n_category = models.CharField(max_length=100)
    date_inserted = models.DateTimeField(auto_now_add=True)
    date_last_update = models.DateTimeField(auto_now_add=True)

class Directory(models.Model):
    website_name = models.CharField(max_length=200)
    website_url = models.URLField()
    website_position = models.IntegerField()
    activated = models.BooleanField()
    date_inserted = models.DateTimeField(auto_now_add=True)
    date_last_update = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category)

How can I do this in the shell?

Best Regards,

Was it helpful?

Solution

Where directory is an instance of Directory:

directory.categories.values_list('n_category', flat=True)

See the docs on values_list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top