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

  • Home
  • SEARCH
  • 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 453919
In Process

The Archive Base Latest Questions

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

I have the following Django models: – class Company(models.Model): name = models.CharField(max_length=50) is_active =

  • 0

I have the following Django models: –

class Company(models.Model):
    name = models.CharField(max_length=50)
    is_active = models.BooleanField(db_index=True)

class Phase(models.Model):
    company = models.ForeignKey(Company)
    name = models.CharField(max_length=50)
    is_active = models.BooleanField(db_index=True)

class Process(models.Model):
    company = models.ForeignKey(Company)
    name = models.CharField(max_length=50)    
    phases = models.ManyToManyField(Phase, through='ProcessPhase')
    is_active = models.BooleanField(db_index=True)

class ProcessPhase(models.Model):
    process = models.ForeignKey(Process)
    phase = models.ForeignKey(Phase)
    order = models.PositiveIntegerField(help_text="At what step of your process will this phase occur?", unique=True)

A “company” has its “processes” and “phases”. A process (of a company) is comprised of one or more phases (of the company). Each phase associated with a process has an “order”. The requirement is that: –

  1. in a particular process of a company, a phase can appear only once;
  2. also “phase A” and “phase B” in a process cannot have the same order.

So I need to know: –

a) how to specify some “unique”s in the model definition to fulfill the above requirements;

b) what uniqueness, if any, is automatically implied by a ManyToManyField?

  • 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-12T22:13:09+00:00Added an answer on May 12, 2026 at 10:13 pm

    In your case, since you say “A process (of a company) is comprised of one or more phases (of the company)”, it seems like you should have a structure like:

    Company <----* Process <----* Phase
    

    Company has its Processes, Process has its Phases. It’s not really a ManyToMany relation, it’s OneToMany (Process has many Phases, but each Phase is connected to one Process).

    If so, you should have

    class Phase(models.Model):
        process = models.ForeignKey(Process, null=True) # based on your comment, if a Phase does not belong to a Process, leave it null.
        phase = models.ForeignKey(Phase)
        order = models.PositiveIntegerField(help_text="At what step of your process will this phase occur?")
    
    
        class Meta:
            unique_togather = ("process", "order")
    

    The unique_together in Meta class is what you want, I think. It enforces both in admin and on database level the uniqueness of those 2 fields together.


    edit:
    (ForeignKey field can be null – see this)


    based on your comment:

    Don’t use ManyToMany, as it auto-generates the “table-in-the-middle”, while you need it specific for your needs. Instead, try defining the different model (together with your Company, Phase and Process):

    class PhaseOrder(models.Model):
      process = models.ForeignKey(Process)
      phase = models.ForeignKey(Phase)
      order = models.PositiveIntegerField(help_text="At what step of your process will this phase occur?")
      class Meta:
        unique_together = (("process", "order"), ("process", "phase"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
If I have the following model in django; class MyModel(models.Model): name = models.CharField(max_length=50) fullname
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,
I have the following model in django: class Node(models.Model): name = models.CharField(max_length=255) And this
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have the following Django model: class Make: name = models.CharField(max_length=200) class MakeContent: make
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have following models relation: class Section(models.Model): section = models.CharField(max_length=200, unique=True) name = models.CharField(max_length=200,

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.