Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:19:26+00:00 2026-05-31T01:19:26+00:00

I need to understand a bit better how do FK/m2m relationships work. I’ve prepared

  • 0

I need to understand a bit better how do FK/m2m relationships work.
I’ve prepared Images model for uploading images and it has an additional feature – it can be categorized by adding to a specific gallery (m2m relation to gallery).

To access gallery name I just had to do a query set for example:

Images.objects.filter(gallery__gallery_name = '')

I’d like to reverse the query a little bit so from Gallery model I can access pictures which are in specific gallery (gallery_name).

How I can do that?

Models:

class Images(models.Model):
   image = models.ImageField(upload_to=update_filename, blank=True, null=True, verbose_name="Obrazek")
   gallery = models.ForeignKey('Gallery', blank=True, null=True)

class Gallery(models.Model):
    gallery_name = models.CharField(max_length=128)
    gallery_description = models.TextField(blank=True, null=True)

View:

def index(request):
    p = Gallery.objects.filter(gallery_name="main").order_by('-id')
    return TemplateResponse(request, 'gallery.html', 
                                    {'gallery': p,
                                    },)

Template:

{% for n in gallery.all %}
<h2 class="center">{{n.gallery_name}}</h2>
<hr>
    {% for n in gallery.images_set %}
       <div class="grid_4">
        {{ n.image }}
       </div>
{% endfor%}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:19:27+00:00Added an answer on May 31, 2026 at 1:19 am

    Try something along the lines of:

    # models.py
    class Gallery(models.Model):
        name = models.CharField(max_length=30)
        description = models.CharField(max_length=200, null=True)
        images = models.ManyToMany(Image)
    
    class Image(models.Model):
        title = models.CharField(max_length=30)
        caption = models.CharField(max_length=200, null=True)
        image = models.ImageField(upload_to=SOMEPLACE_FOR_MEDIA)
    

    From here you should be able to do things like:

    image = Image.objects.get(title="Girl Holding Cheese")
    related_galleries = image.gallery_set.all()
    

    or something similar as needed to pull what you want. The same goes the other way. To pull all images in a gallery you would do

    gallery = Gallery.objects.get(name="Cheesy Wimmin")
    related_images = gallery.images.all()
    

    Though the assignments at the end aren’t necessary, I usually just pass gallery.images.all() or image.gallery_set.all() directly. Note the “_set” at the end of the reference from the object that does not contain the M2M definition.

    On the subject of direct usage, you can do compound references like

    Image.objects.get(title="Girl Holding Cheese").gallery_set.all()
    

    as well, but you have to decide when this makes code more clear and concise and when it just makes it more confusing to read later.

    I hope this put you in the right direction.

    Update

    In your comment below you noticed that you cannot do

    images = Images.objects.filter(gallery_set="Cheesy Wimmins")
    related_galleries = images.gallery_set.all()
    

    This is because you would be trying to filter() or all() on a queryset, not an individual model. So to make this work you can use a for loop in your template. Something like

    # views.py
    galleries = Gallery.objects.all()
    return render(request, 'some/template.html', {'galleries': galleries})
    

    And then

    <!-- templates/some/template.thml -->
    {% for gallery in galleries %}
        <div class="gallery">
        <h2>{{ gallery.name }}</h2>
        {% for item in gallery.images.all %}
            <div class="image">
                {{ item.image }}
            </div>
        {% endfor %}
        </div>
    {% endfor %}
    

    or something like this. Of course, you need to do whatever formatting steps you want to make this look right, but that’s a way to get at your data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the need to test a class that has logic (for instance, one
I am trying to understand something a bit better with being new to C#,
I need to understand the differences between windows main/mdi/child/dialogs.... how win32 messages should be
I need to understand how to use the generic Delphi 2009 TObjectList . My
I need to understand the working of this particular program, It seems to be
What are the top 3 main concepts in WPF that you need to understand
I don't know if this is a right question, but i need to understand
I edited this question after i found a solution... i need to understand why
I don't understand the need for self-joins. Can someone please explain them to me?
I understand that I need to use LoadLibrary(). But what other steps do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.