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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:59:54+00:00 2026-05-28T05:59:54+00:00

I want to automatically add some items to a model’s ManyToMany field when it

  • 0

I want to automatically add some items to a model’s ManyToMany field when it is saved in the Django admin UI. However, calling model.many2manyfield.add() is failing silently

my django app’s models.py

...
class Dashboard(models.Model):
    name = models.TextField()
    url = models.TextField()
    graphs = models.ManyToManyField(Graph, null=True, blank=True)

my django app’s admin.py

from models import Dashboard
...
class DashboardAdmin(GraphAdmin):
    def save_model(self, request, dashboard, form, change):
        dashboard.save()
        graphite_util.sync_dashboard_graphs(dashboard)

...
admin.site.register(Dashboard, DashboardAdmin)

from graphite_util.py:

def sync_dashboard_graphs(dashboard):
    print "Syncing %s " % dashboard

    ...
            graph = None
            try:
                graph = Graph.objects.get(url=url)
            except Graph.DoesNotExist:
                graph = Graph()
                graph.url = url
                graph.save()
            print "adding graph %s to dashboard %s" %(graph, dashboard)
            dashboard.graphs.add(graph) #TODO: why does this fail silently?

Is what I want to do somehow illegal? If so, are there other options?

Thanks!

  • 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-28T05:59:55+00:00Added an answer on May 28, 2026 at 5:59 am

    In principle it should work; however, you’ve got some inconsistencies in your code. For example, you’re looking up the graph based on url. You’ve redacted part of your code, so it’s possible you created the variable in the missing part, but I’m not sure where you would have gotten that info from; you have no access to the request from this method.

    However, you could pass the request in to the method from where it’s called in save_model because the request is available there.

    Additionally, if you’re simply going to create a graph in the except block, you’re better off using get_or_create.

    graph = Graph.objects.get_or_create(url=url)
    

    It basically does the same thing, but much more concisely.

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

Sidebar

Related Questions

I'm doing a Unity-iPhone project, but want to automatically add some native code and
I want to add some Ajax -niceness to my Django-coded website. In my Django
I want to use jquery to automatically fill some details in a web site
I want to add some behavior to a certain class at runtime. I know
I want to automatically add files to an Xcode project that is created from
I want to add some methods to the context retrieved from a canvas object.
I have a Products class. Now I want to add some kind of discount
i want to automatically run a simple shell script at linux startup i'm working
I want to automatically fit time series returns into a NIG distribution. With nigfit()
I get poorly formatted invoice-related email from my payment gateway. I want to automatically

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.