How list ManyToManyField in interactive shell?
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,
Where directory
is an instance of Directory
:
directory.categories.values_list('n_category', flat=True)
See the docs on values_list
.
精彩评论