Найти - Пользователи
Полная версия: ManyToManyFields
Начало » Django » ManyToManyFields
1
skripka696
как пройтись в цикле по полю ManyToMany

     list_category = []
    for k,v in category_name.items():
        if category_name[k] is v:
            list_category.append(v)
        print list_category 
Ошибка:AttributeError: ‘ManyToManyField’ object has no attribute ‘items’

list_category = []
for i in note_category:
    list_category.append(i)
    print list_category
Ошибка: TypeError: ‘ManyToManyField’ object is not iterable
FishHook
Где модели? где запросы?
skripka696
FishHook
from django.db import models
from django.contrib.auth.models import User
class Color(models.Model):
    code = models.CharField(max_length=30)
    color_name = models.CharField(max_length=30)
    def __unicode__(self):
        return '{0}'.format(self.color_name)
class Category(models.Model):
    category_name = models.CharField(max_length=20)
    def __unicode__(self):
        return '{0}'.format(self.category_name)
class Lable(models.Model):
    lable_name = models.CharField(max_length=20) 
    def __unicode__(self):
        return '{0}'.format(self.lable_name)
class Media_Note(models.Model):
    my_file = models.FileField(upload_to = 'media/', default="note.jpg")
    def __unicode__(self):
        return '{0}'.format(self.my_file)
class Note(models.Model):
    title = models.CharField(max_length=30)
    content = models.CharField(max_length=255)
    color = models.ForeignKey(Color, blank=True , null=True)
    note_category = models.ManyToManyField(Category)
    note_lable = models.ManyToManyField(Lable)
    note_media = models.ManyToManyField(Media_Note)
    user = models.ForeignKey(User)
    # list_category = []
    # for k,v in note_category.all():
    #     if note_category[k] is v:
    #         list_category.append(v)
    #     print list_category
    def __unicode__(self):
        return '{0} - {1}'.format(self.title, self.content)
skripka696
Вообще проблема в том что во вьюшке данные выводит таким образом:

Category: my_rest.Category.None
Lable : my_rest.Lable.None
Media : my_rest.Media_Note.None

 {% for o in note_list %}
        <ul class="list-group">
        <li class="list-group-item"><a href="{%url 'get_id_list' o.pk%}">{{o.pk}}:    Title: {{o.title}}<br>
        Category: {{o.note_category}}<br>
        Lable : {{o.note_lable}}<br>
        Media : {{o.note_media}}<br>
        </li>
        </ul>
        </a>
        {% endfor %}
FishHook
notes = Note.objects.filter()
for note in notes:
    labels = note.note_lablel_set.all()
skripka696
FishHook
по прежнему те же шибки!
AttributeError: ‘ManyRelatedManager’ object has no attribute ‘set_all’


вот таким образом выводятся:
notes = Note.objects.filter()
for note in notes:
    labels = note.note_lable
    print labels
my_rest.Lable.None
my_rest.Lable.None
my_rest.Lable.None
my_rest.Lable.None
FishHook
skripka696
object has no attribute ‘set_all’
покажите, где в моём коде вы видите set_all
skripka696
FishHook
В случае вашего кода:
AttributeError: ‘Note’ object has no attribute ‘note_lable_set’
FishHook
То вы его прям так и копируете, с пустыми скобками после filter?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB