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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:13:31+00:00 2026-06-02T07:13:31+00:00

So I’ve been browing around the web a bit and I can’t quite find

  • 0

So I’ve been browing around the web a bit and I can’t quite find what I’m looking for. First off Say I have

class Contact(models.Model):
    nickname = models.CharField(max_length=25)
    number = models.CharField(max_length=25)
    note = models.TextField()

class Telephone(models.Model):
    contact = models.ForeignKey('Contact')

So how would i go about saving a nickname/number/not and then be able to find all of these based on some label that I could give them all?

Say I wanted to have it be that when I used Telephone.objects.filter(contact=jill) all of the information based on the instance of jill would be provided? what if i wanted to be able to edit jill and give her more than one number via different saves is this possible? (but jill isn’t her nickname its the label that I put on there.) Sort of a bad example but say I just want to find all of the information in Contact via some named instance that is filterable via Telephone is that possible? or is Foreignkey not what I’m looking for?

  • 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-06-02T07:13:35+00:00Added an answer on June 2, 2026 at 7:13 am
    Telephone.objects.filter(contact__nickname='jill')
    

    See the documentation on making queries

     what if i wanted to be able to edit jill and give her more than one number via
     different saves is this possible?
    

    Yes it is. To give one thing in contact objects multiple telephone numbers, there are two ways – a One-To-One relationship via a ForeignKey, or a many-to-many relationship. I suggest you read through the models topic in the documentation.

    Edit

    You can give Jill more than one phone number on each save in the model layout that you have.

    Suppose Jill has a new line, so you want to add a new phone number. The way you have it structured now, you would first have to go and find ‘Jill’ in Contacts, and then give her a new phone number:

    jill = Contact.objects.get(nickname='jill') # assuming you only have one jill
    new_num = Telephone.objects.create(contact=jill,number=5551212)
    

    Now if you want all of Jill’s contacts, you would do:

    call_jill = Telephone.objects.filter(contact=jill) # if you already pulled jill
    call_jill = Telephone.objects.filter(contact__nickname='jill')
    

    The other way to do it would be with a ManyToMany:

       class Telephone(models.Model):
          number = models.PhoneNumberField()
    
       class Contact(models.Model):
          # your normal fields
          phones = models.ManyToMany(Telephone)
    

    Now, you can do:

       jills_phones = Contact.objects.get(nickname='jill').phones.all()
    

    To add a new phone:

       jill = Contact.objects.get(nickname='jill')
       jill.phones.add(Telephone.objects.create(number=5551212))
       jill.save()
    

    You can also do the reverse, find out whose phone this is:

       phone = Telephone.objects.get(number=5551212)
       whose_is_it = phone.contact_set.all()
    

    So what’s the difference?

    They both sound similar, but the differences are subtle. A simple analogy is easy. One Manufacturer can have many Cars, but one Car can only have one Manufacturer – this is a one-to-one between Car and Manufacturer.

    A Pizza can have many toppings, and each topping can be on multiple pizzas. In this case, Pizza has ManyToMany relationship with Toppings.

    You might be thinking that I can do that with a ForeignKey. You are right, but consider this scenario:

    Suppose you start off and you have no toppings. So you create olives as a topping and then add it to a pizza. Great.

    Now you want the same pizza to have green peppers as well. Now you are stuck, because one pizza can only have one topping (due to ForeignKey).

    So you end up creating a third model, which holds a reference to a pizza and a topping. So instead of putting toppings on pizzas, you put them on this other model:

    class Pizza(models.Model):
       topping = models.ForeignKey('Topping')
    
    class Topping(models.Model):
       name = models.CharField(max_length=100)
    
    class AssembledPizza(models.Model):
       topping = models.ForeignKey('Topping')
       pizza = models.ForeignKey('Pizza')
    

    However, if you did it the correct way with a ManyToMany, you have:

    class Pizza(models.Model):
       toppings = models.ManyToMany('Topping')
    
    class Topping(models.Model):
       name = models.CharField(max_length=100)
    

    Then you would do:

    Here I am assuming you are a new restaurant and have no toppings. See the documentation for create for more information.

    veggie = Pizza()
    veggie.toppings.add(Topping.objects.create(name='Olives'))
    veggie.toppings.add(Topping.objects.create(name='Peppers'))
    veggie.save()
    

    I hope this clarifies it a bit.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.