B
B
Bulachka2020-05-04 21:51:13
Django
Bulachka, 2020-05-04 21:51:13

How to pull data from fields connected via ManyToMany in django DetailView?

I have three models: main "Stones", linked to "Types" via ForeignKey and to "Mentions" via Many-to-Many. I want to pass them to the template using the DetailView class, but only the 'Stones' and 'Types' data is shown, but not from '

Mentions'.models.py (required part)

class StonesManager(models.Manager):
def all_with_prefetch_mentions(self):
    qs = self.get_queryset()
    return qs.prefetch_related('mentions')


class Stones(models.Model):
title = models.CharField(max_length=30, verbose_name='Назва')
place = models.TextField(verbose_name='Месцазнаходжанне')
legend = models.TextField(null=True, blank=True, verbose_name='Легенда')
typ = models.ForeignKey('Typ', null=True, on_delete=models.PROTECT, verbose_name='Тып')
objects = StonesManager()

class Typ(models.Model):
name = models.CharField(max_length=20, db_index=True, verbose_name='Назва тыпу')
def __str__(self):
    return self.name

class StonesImage(models.Model):
image = models.ImageField(upload_to=stones_directory_path_with_uuid)
uploaded = models.DateTimeField(auto_now_add=True)
stones = models.ForeignKey('Stones', on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

class Mentions(models.Model):
work = models.TextField(verbose_name='Праца')
year = models.PositiveIntegerField(blank=True, null=True)
sacral_objects = models.ManyToManyField(Stones, related_name='mentions', verbose_name='Сакральны аб\'ект')


views.py
class StonesDetail(generic.DetailView):
queryset = Stones.objects.all_with_prefetch_mentions()
template_name = 'volumbf/stone_detail.html'

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['image_form'] = self.stones_image_form()
    return context

def stones_image_form(self):
    if self.request.user.is_authenticated:
        return StonesImageForm()
    return None


template
1. Everything looks great

<h2> {{ stones.title}} </h2>
  <p>{{ stones.legend }} </p>
  <p>{{ stones.place }}</p>
  <p>Typ: <a href="/volumbf/{{ stones.typ.pk }}/">{{ stones.typ.name }}</a></p>


2. Nothing is displayed at all
{% for work in stones.mentions.work %}
    		<p><a href="{% url 'work_detail' work.pk %}"> {{ stones.mentions.work }}</a></p>
  {% endfor %}
   <ul class="stone-image list-inline" >
        {% for i in object.stonesimage_set.all %}
            <li class="list-inline-item" >
                <img scr="{{ i.image.url }}">
            </li>
        {% endfor %}
    </ul>
    {% if image_form %}
        <div>
            <h2 >Upload New Image</h2>
        <form method="post"
              enctype="multipart/form-data"
              action="{% url 'StonesImageUpload' stones_id=object_id %}" >
            {% csrf_token %}
            {{ image_form.as_p }}
            <p ><button class="btn btn-primary"></button> </p>
        </form>


I believe I'm naming stones.mentions.work and object.stonesimage_set.all incorrectly, but that's not accurate.

I would be glad for any hint!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Bulachka, 2020-05-09
@Bulachka

The error was indeed in the template, the correct code is below

{% for work in stones.mentions.all %}
        <p><a href="{% url 'work_detail' work.pk %}"> {{ work }}</a></p>
{% endfor %}

{% for i in object.stonesimage_set.all %}
            <li class="list-inline-item" >
                <img src="{{ i.image.url }}" alt="{{ i.id }}" >
            </li>
{% endfor %}

action="{% url 'StonesImageUpload' stones.pk %}" >

V
Vladimir, 2020-05-05
@vintello

what does the documentation say about it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question