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 1066717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:03:29+00:00 2026-05-16T20:03:29+00:00

Can someone tell me, how I can access all contacts relating to a specific

  • 0

Can someone tell me, how I can access all contacts relating to a specific group? I’m new to Django and did this (according to the docs):

def view_group(request, group_id):
    groups = Group.objects.all()
    group = Group.objects.get(pk=group_id)
    contacts = group.contacts.all()
    return render_to_response('manage/view_group.html', { 'groups' : groups, 'group' : group, 'contacts' : contacts })

“groups” is for something different, I tried it with “group” and “contacts” but get a

'Group' object has no attribute 'contacts'

exception.

Here’s the model I’m using

from django.db import models

# Create your models here.

class Group(models.Model):
    name = models.CharField(max_length=255)
    def __unicode__(self):
            return self.name

class Contact(models.Model):
    group = models.ForeignKey(Group)
    forname = models.CharField(max_length=255)
    surname = models.CharField(max_length=255)
    company = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    zip = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    tel = models.CharField(max_length=255)
    fax = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    url = models.CharField(max_length=255)
    salutation = models.CharField(max_length=255)
    title = models.CharField(max_length=255)
    note = models.TextField()
    def __unicode__(self):
            return self.surname

Thanks in advance!

EDIT: Oh and can someone tell me how I can add a contact to a group?

  • 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-16T20:03:29+00:00Added an answer on May 16, 2026 at 8:03 pm

    One way:

    group = Group.objects.get(pk=group_id)
    contacts_in_group = Contact.objects.filter(group=group)
    

    Another, more idomatic, way:

    group = Group.objects.get(pk=group_id)
    contacts_in_group = group.contact_set.all() 
    

    contact_set is the default related_name for the relation as shown in the related objects docs.

    If you wanted to, you could specify your own related_name, such as related_name='contacts' when defining the field and then you could do group.contacts.all()

    To add a new Contact to a group, you just need to assign the relevant group to contact via the group field and save the Contact:

    the_group = Group.objects.get(pk=the_group_id)
    newcontact = Contact()
    ...fill in various details of your Contact here...
    newcontact.group = the_group
    newcontact.save() 
    

    Sounds like you’d enjoy reading the free Django Book to get to grips with these fundamentals.

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

Sidebar

Related Questions

Can someone tell me why this processes all the files and then does it
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
Can someone tell me why this http://hupcapstudios.com/projects/calendar.html won't load in IE? The error message:
can someone tell me if AcrobatAccessLib (Acrobat Access 3.0 Type Library) in com references
Can someone tell me why this string won't unserialize? I've tried just about everything
Can someone tell me how to copy an MS Access table's table structure and
can someone tell me the order in which a thread starts to execute?. I
Can someone tell me how can I select the Row under the one on
Can someone tell me why I'm getting successCallback is not a function in my

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.