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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:52:39+00:00 2026-05-12T18:52:39+00:00

Pardon the excessive amount of code, but I’m not sure if I can explain

  • 0

Pardon the excessive amount of code, but I’m not sure if I can explain my question otherwise

I have a Django project that I am working on which has the following:

class Project(models.Model):
    name = models.CharField(max_length=100, unique=True)
    dir  = models.CharField(max_length=300, blank=True, unique=True )

    def __unicode__(self):
        return self.name;

class ASClass(models.Model):
    name      = models.CharField(max_length=100)
    project   = models.ForeignKey(Project, default=1)

    def __unicode__(self):
        return self.name;

class Entry(models.Model):
    project     = models.ForeignKey(Project, default=1)
    asclasses   = models.ManyToManyField(ASClass)

Here’s the question:

Is there a way, without overriding the save function of the model, to make it so that entries only allow classes which have the same project ID?

***********************************************************Begin Edit**********************************************************
To be clear, I am not opposed to overriding save. I actually already overrode it in this case to provide for a property not listed above. I already know how to answer this question by simply extending that override, so simply saying, “You could override save” won’t be helpful.

I’m wondering if there isn’t a better way to accomplish this, if there is a Django native implementation, and if the key type already exists.

***********************************************************End Edit***********************************************************

Is there a way to do this in Postgresql as well?

(For good measure, here is the code to create the tables in the Postgresql)
This has created the following tables:

CREATE TABLE blog_asclass
(
  id serial NOT NULL,
  "name" character varying(100) NOT NULL,
  project_id integer NOT NULL,
  CONSTRAINT blog_asclass_pkey PRIMARY KEY (id),
  CONSTRAINT blog_asclass_project_id_fkey FOREIGN KEY (project_id)
      REFERENCES blog_project (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)

CREATE TABLE blog_entry
(
  id serial NOT NULL,
  project_id integer NOT NULL,
  build_date timestamp with time zone NOT NULL,
  CONSTRAINT blog_entry_pkey PRIMARY KEY (id),
  CONSTRAINT blog_entry_project_id_fkey FOREIGN KEY (project_id)
      REFERENCES blog_project (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)

CREATE TABLE blog_entry_asclasses
(
  id serial NOT NULL,
  entry_id integer NOT NULL,
  asclass_id integer NOT NULL,
  CONSTRAINT blog_entry_asclasses_pkey PRIMARY KEY (id),
  CONSTRAINT blog_entry_asclasses_asclass_id_fkey FOREIGN KEY (asclass_id)
      REFERENCES blog_asclass (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT blog_entry_asclasses_entry_id_fkey FOREIGN KEY (entry_id)
      REFERENCES blog_entry (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT blog_entry_asclasses_entry_id_key UNIQUE (entry_id, asclass_id)
)

CREATE TABLE blog_project
(
  id serial NOT NULL,
  "name" character varying(100) NOT NULL,
  dir character varying(300) NOT NULL,
  CONSTRAINT blog_project_pkey PRIMARY KEY (id),
  CONSTRAINT blog_project_dir_key UNIQUE (dir),
  CONSTRAINT blog_project_name_key UNIQUE (name)
)
  • 1 1 Answer
  • 1 View
  • 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-12T18:52:40+00:00Added an answer on May 12, 2026 at 6:52 pm

    You could use the pre_save signal and raise an error if they do no match… The effect would be similar to overridding save (it gets called before save)

    The problem is creating/deleting/updating the many-to-many relation will not trigger save (or consequentially pre_save or post_save)

    Update

    Try using the through argument on your many-to-many relation

    That lets you manually define the intermediary table for the m2m relation, which will give you access to the signals, as well as the functions.

    Then you can choose signals or overloading as you please

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

Sidebar

Related Questions

Pardon the vague question, but I've just inherited a project to build a couple
Do pardon the simplicity of the question: mother Google seems not to the have
Pardon the dumb newbie question here; web programming isn't my forte... (blush) I have
Pardon the simple question, but I am pulling my hair out trying to get
Pardon me, if it is a silly question. Can you anyone tell me how
Pardon for this, i just get this error in console but i can't see
Pardon me for the woolly question - I'm not really that familiar with OLAP
Pardon me, this really is a noob question but please understand that I do
Pardon the total newbiew question but why is @game_score always nil? #bowling.rb class Bowling
Pardon me for this long story, but I think the question merits it. I

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.