Home > Blockchain >  using .all() gives me everything inside the Primary Model How can i change it to gives what i needed
using .all() gives me everything inside the Primary Model How can i change it to gives what i needed

Time:03-03

I have 2 models Album and Primary when i go to the Albums Templates and clicking the viewAlbum templates it's shows me every Students from every Album I have created, instead of showing the only students inside the album i have choosing during creating a new student.

the create album template:

        <div >
        <div >
            <a href="{% url 'post_create' %}">Create Album</a>
        </div>

        <!--Albums-->

        {% for my_album in albums %}
        <div >
            <div  style="width: 18rem;">
                <img src="{{ my_album.image.url }}" alt="" >
                <div >
                    <p>{{ my_album.name }}</p>
                    <br>
                    <a href="{% url 'view-Album' my_album.pk %}">viewAlbum</a>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

the viewAlbum view:

def viewAlbum(request, pk):
    primaries = Primary.objects.all()

    post = get_object_or_404(Album, id=pk)

    my_album = Album.objects.get(id=pk)
    return render(request, 'viewAlbum.html', {'primaries': primaries, 'post':post, 
    'my_album':my_album})

the students templates:

    <div >
    <div >
        <div >
            <a href="{% url 'students' %}" >Create Students</a>
        </div>
    </div>
</div>
<br>
<div >
    <div >
        {% for prima in primaries %}
        <div >
            <div  style="width: 18rem;">
                <img src="{{ prima.profilePicture.url }}" alt="" >

                <div >
                    <p>{{ prima.firstName }}</p>
                    <br>
                    <a href="{% url 'view-Student' prima.pk %}">view Students</a>
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
</div>

the models:

class Album(models.Model):
    image = models.ImageField(null=False, blank=False)
    name = models.CharField(max_length=100, null=False, blank=False)

    def __str__(self):
        return self.name       

class Primary(models.Model):
    profilePicture = models.ImageField(blank=False, null=False)
    firstName = models.CharField(max_length=25, blank=False, null=False) 
    sureName = models.CharField(max_length=25, blank=False, null=False) 
    lastName = models.CharField(max_length=25, blank=False, null=False)  
    address = models.CharField(max_length=50, blank=False, null=False)
    classOf = models.CharField(max_length=20, blank=False, null=False)
    yearOfGraduations = models.CharField(max_length=20, blank=False, null=False)
    hobbies = models.TextField(blank=False, null=False)
    dateOfBirth = models.CharField(max_length=20)
    year = models.ForeignKey(Album, on_delete=models.CASCADE)

    def __str__(self):
        return self.firstName

the urls.py

path('viewAlbum/<int:pk>/', views.viewAlbum, name='view-Album'),
path('view/<int:pk>/', views.viewStudent, name='view-Student'),

CodePudding user response:

change this code

def viewAlbum(request, pk):
    primaries = Primary.objects.all()

    post = get_object_or_404(Album, id=pk)

    my_album = Album.objects.get(id=pk)
    return render(request, 'viewAlbum.html', {'primaries': primaries, 'post':post, 
    'my_album':my_album})

to this code

def viewAlbum(request, pk):
    post = get_object_or_404(Album, id=pk)
    my_album = Album.objects.get(id=pk)
    
    primaries = Primary.objects.filter(year_id=my_album.pk)
    # another way: 
    # primaries = my_album.primary_set.all()

    return render(request, 'viewAlbum.html', {'primaries': primaries, 'post':post, 
    'my_album':my_album})
  • Related