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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:12:25+00:00 2026-05-13T08:12:25+00:00

I have a problem with deleting my objects. I wrote a delete method which

  • 0

I have a problem with deleting my objects. I wrote a delete method which get a list of IDs of objects to be deleted. This works fine for objects and for objects with foreign keys, but it fails if i have a OneToOneField relationship to the object which i want to delete.

This is my delete method:

@login_required
def delete_objects(request, model, selected_ids):
    '''
    capsulate a bulk delete method
    delete all objects found for the given model
    fails silently since model.delete() always fails silently
    '''
    object_list = model.objects.filter(pk__in=selected_ids)
    count = object_list.count()
    if count == 1:
        name = model._meta.verbose_name.title()
    else:
        name = model._meta.verbose_name_plural.title()
    object_list.delete()
    request.user.message_set.create(message='Successfully deleted %s %s' % (count,name))
    return

Till now I just tested it with Contact objects. Now I added a PhoneNumber model to my models. The PhoneNumber model has a OneToOneField relationship to the Contact model. If no PhoneNumber object or one PhoneNumber object is assigned to the Contact object i can delete it. But if I relate more than one PhoneNumber object to a Contact object i get an error.

This is the error message that i get:

MultipleObjectsReturned at /crm/contacts/
get() returned more than one PhoneNumber -- it returned 3! Lookup parameters were {'contact__pk': 4L}
Request Method: POST
Request URL:    http://127.0.0.1:8000/crm/contacts/
Exception Type: MultipleObjectsReturned
Exception Value:    
get() returned more than one PhoneNumber -- it returned 3! Lookup parameters were {'contact__pk': 4L}

I read in the django docs

deleting objects

that “any objects which had foreign keys pointing at the object to be deleted will be deleted along with it.” This is the goal I want to accomplish.. but now i get an error. All I want is the objects to be deleted 😀

Actually is this maybe a design problem? Is it wrong when I relate more than one PhoneNumber object to a Contact object with a OneToOneField relationship? I have chosen OneToOneField since the phone number is unique and should be related to only one contact.

  • 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-13T08:12:26+00:00Added an answer on May 13, 2026 at 8:12 am

    Is it wrong when I relate more than one PhoneNumber object to a Contact object with a OneToOneField relationship?

    You got it. Change the relationship to a foreign key in PhoneNumber and set the unique option for the PhoneNumberField.

    Should end up like this:

    class PhoneNumber(models.Model):
        # some fields...
        number = PhoneNumberField(unique=True)
        contact = models.ForeignKey(Contact, related_name="phone_numbers")
    

    Also, just to make sure I understand how you want the constraints set up: This example assumes that a phone number will only appear once in the entire database and therefore also that only one contact can have that phone number.

    In case you wanted something less restrictive, use unique_together in the model’s Meta options like so:

    class PhoneNumber(models.Model):
        # some fields...
        number = PhoneNumberField()
        contact = models.ForeignKey(Contact, related_name="phone_numbers")
    
    class Meta:
        unique_together = ("number", "contact")
    

    This will allow multiple contacts to have the same phone number, but a single contact cannot have that number more than once.

    http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

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

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer scipy.stats.scoreatpercentile(arr,per) returns the value at a given percentile: import scipy.stats… May 15, 2026 at 4:21 pm
  • Editorial Team
    Editorial Team added an answer Sure. Just return a HttpResponse with a 401 status code,… May 15, 2026 at 4:21 pm
  • Editorial Team
    Editorial Team added an answer Be sure that you're only calling dismissModalViewControllerAnimated: once. I have… May 15, 2026 at 4:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.