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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:24:20+00:00 2026-06-02T06:24:20+00:00

I have a simple data model in Django, with classes Department and Role .

  • 0

I have a simple data model in Django, with classes Department and Role. (For example sake.)

A Department has a one-to-many relationship with Role, and the Role class contains an ID that is being used to identify particular instances. For example:

> Department: Sales
>> Role (1): Accounts
>> Role (3): Closing

> Department: Maintenance
>> Role (2): IT
>> Role (4): Equipment

The downside to this is that each role is using its own global ID. So if you were to create a large number of Sales roles, and then create an additional Maintenance role, that new Maintenance role would have an incredibly large ID, and would seem “out-of-sync.”

Thus, I would like to create a department-specific ID for each Role as well, like so:

class Role(models.Model):
    department_id = models.IntegerField()

The downside is that I can’t find a way to either:

1) Have existing Roles transfer their existing IDs to the new department_id field, leaving the gaps but preventing future gaps from appearing:

> Department: Sales
>> Role (1): Accounts
>> Role (3): Closing
>> Role (4): *New Sales Role*

> Department: Maintenance
>> Role (2): IT
>> Role (4): Equipment
>> Role (5): *New Maintenance Role*

2) Or, dynamically assign the default value to a method that scans the table for all the Roles of each department, and assigns them new IDs:

> Department: Sales
>> Role (1): Accounts
>> Role (2): Closing
>> Role (3): *New Sales Role*

> Department: Maintenance
>> Role (1): IT
>> Role (2): Equipment
>> Role (3): *New Maintenance Role*

I’m using South to handle schema migrations.

  • 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-06-02T06:24:22+00:00Added an answer on June 2, 2026 at 6:24 am

    You will need three migrations (Taking approach two):

    1. Add a new field to Roles (I’ll call it user_id since it’s the user visible id I assume…) — This is done via a schema migration
    2. Create a data migration which loops over all departments, fetches the roles for each department and renumbers them into the new user_id field (a simple counter in the inner loop should suffice, probably order the roles by the current primary key)
    3. Add another schema migration which adds a unique_together constraint to department_id, user_id — you don’t want duplicate user_ids in one department.

    Add a new default method to the user_id field which fetches the data as needed (Take care to not run into ugly edge cases — I would use a SlugField with the role name instead of integer ids)

    You obviously won’t get rid of the existing pk since Django doesn’t support compound primary keys (like over department_id, user_id) but you don’t have to display it anywhere.

    That said; if it’s just to get “nice” ids I would choose slugs instead of numbers.

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

Sidebar

Related Questions

I have a couple of simple objects that have a many-to-many relationship. Django has
I have a simple data model of two tables, email and recipients, email can
I have a simple Django-Piston Handler that creates a new instance of a model
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a simple Core Data model with two string attributes (size and category).
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
Suppose I have to develop a simple data model in Java for Order ,
I'm using core data and I have a simple two-entity model. My navigation controller
I have a simple model define in Django. I want to select an Object
I'm bulk loading data into a django model, and have noticed that the number

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.