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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:21:39+00:00 2026-05-28T07:21:39+00:00

I have an AdminModel that allows users to save with any of the fields

  • 0

I have an AdminModel that allows users to save with any of the fields left empty. However, I don’t want them to be able to save when all fields are empty. I’ve implemented this using the following:

def save_model(self, request, obj, form, change):
    if form.has_changed():
        obj.save()
    else:
        pass

However, the Model has a M2M relationship with another Model, so I get the following error when I hit save:

instance needs to have a primary key value before a many-to-many relationship can be used.

What do I need to add to this method to stop it trying to create the M2M relationship?

Edit:

I’ve added hacky workaround, but I’d be interested in knowing if there’s a better way of doing it. The workaround was to change the else so it changes the ModelAdmin’s field attribute so that it doesn’t contain the field for the M2M relationship – this prevents it from trying to make the relationship. This is not only hacky, but also only works after it’s failed to save the model once and thrown an error…

  • 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-28T07:21:40+00:00Added an answer on May 28, 2026 at 7:21 am

    You can’t – save_m2m is called after save_model. The docs specifically say this hook is not for veto purposes.

    ModelAdmin.save_model() and ModelAdmin.delete_model() must save/delete
    the object, they are not for veto purposes, rather they allow you to
    perform extra operations.

    What you need is form validation to prevent save_model from triggering. Somehow, you need to detect when all fields are left blank.

    class MyForm(forms.ModelForm):
        class Meta:
            model = MyModel
    
        def clean(self):
            if not any(self.cleaned_data.values()):
                raise forms.ValidationError("All fields cannot be blank!")
            return self.cleaned_data
    
    class MyAdmin(admin.ModelAdmin):
        form = MyForm
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have some relatively large legacy method that I would like to refactor. It
I have the following Actionresult. [HttpPost] public ActionResult Logon(AdminModel model) { if (model.UserName ==
I have the following code that checks the userName & userPassword is correct then
Have finally got a responsive site working (of a fashion). What I want to
I want to represent clubs which have multiple members (each member can also join
I have a WCF service and a client, that uses a custom UserNamePasswordValidator for
Have some dates in my local Oracle 11g database that are in this format:
Have any one tried to activate fancybox thumbnail gallery using a button or an

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.