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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:15:06+00:00 2026-05-30T17:15:06+00:00

I’ve got a model that references another model via OneToOneField so that when you

  • 0

I’ve got a model that references another model via OneToOneField so that when you use Django’s built-in delete_selected admin action, the associated model’s data is not deleted. I’d like to write a custom admin action to delete the data in that associated model as well.

Here’s my model:

class Party(models.Model):
    TYPE_CHOICES=(
        ('P','Person'),
        ('O','Organization')
    )
    partyType = models.CharField(max_length=1, choices=TYPE_CHOICES)
    name = models.CharField(max_length=100)
    comment = models.CharField(max_length=500,blank=True)
    accessIdCrossRef=models.IntegerField(blank=True, null=True)
    mailingLists = models.ManyToManyField(MailingList)
    inMainList=models.BooleanField(default=False)
    inSubList=models.BooleanField(default=False)

    class Meta:
        db_table='party'
        ordering=['name',]

    def __unicode__(self):
        return self.name

class Person(models.Model):
    party = models.OneToOneField(Party, editable=False)
    firstName=models.CharField(max_length=60)
    lastName=models.CharField(max_length=60)
    ...


    def save(self):
        if None == self.party :
            print 'Creating party for person'
            p = Party()
            p.partyType = 'P'
            p.save()
            self.party = p

        # Get address to set party name used in list
        city=""
        state=""
        postalCode=""
        try:
            partyAddress = PartyPostalAddress.objects.get(party=self.party)
            address = partyAddress.postalAddress
            city=address.city
            state=address.state
            postalCode=address.postalCode
        except PartyPostalAddress.DoesNotExist:
            pass

        self.party.name = '%s, %s - %s, %s %s' %(self.lastName, self.firstName, city, state, postalCode)
        self.party.save()
        super(Person,self).save()

My assumption was to write a def delete() in my model like this:

def delete(self):
    self.party.delete()
    self.delete()

And an admin action like so:

class PersonAdmin(admin.ModelAdmin):
    list_display = ('lastName','firstName')
    search_fields = ('firstName', 'lastName')
        actions=['really_delete_selected']

    def get_actions(self, request):
    actions = super(PersonAdmin, self).get_actions(request)
    del actions['delete_selected']
    return actions

    def really_delete_selected(self, request, queryset):
    for obj in queryset:
        obj.delete()

    if queryset.count() == 1:
        message_bit = "1 person was"
    else:
        message_bit = "%s people were" % queryset.count()
    self.message_user(request, "%s successfully deleted." % message_bit)
    really_delete_selected.short_description = "Delete selected entries"

That deletes person.party and most of person, but throws an error because person’s party OneToOneField is now empty. The specific error is:

“AssertionError at /admin/common/person/
Party object can’t be deleted because its id attribute is set to None.”

Any ideas? This, this, and this question are related, but only one of them utilizes the OneToOneField and he did that erroneously.

  • 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-30T17:15:07+00:00Added an answer on May 30, 2026 at 5:15 pm

    Got it cleaned up and working!

    My model:

    class Party(models.Model):
        name = models.CharField(max_length=100)
        ...
    
    class Person(models.Model):
        party = models.OneToOneField(Party, editable=False)
        firstName=models.CharField(max_length=60)
        lastName=models.CharField(max_length=60)
    
        def delete(self):
            d = self.party.id
            Party.objects.get(id__exact=d).delete()
    

    My admin:

    class PersonAdmin(admin.ModelAdmin):
        actions=['really_delete_selected']
    
        def get_actions(self, request):
            actions = super(PersonAdmin, self).get_actions(request)
            del actions['delete_selected']
            return actions
    
        def really_delete_selected(self, request, queryset):
            for obj in queryset:
                obj.delete()
    
            if queryset.count() == 1:
                message_bit = "1 person was"
            else:
                message_bit = "%s people were" % queryset.count()
    
            self.message_user(request, "%s successfully deleted." % message_bit)
            really_delete_selected.short_description = "Delete selected entries"
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
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 am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.