2

I have two models:

class University(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=50, null=True, blank=True)
    city = models.CharField(max_length=30, null=True, blank=True)
    country = CountryField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)
    departments = models.ManyToManyField(Department)


class Department(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=50, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)

I want to University have many Departments and vice versa which works fine. But I also want to store more departments individually for one university.

For instance:

University1 has Department1 and Department2.

University2 has Department1.

I want those departments store individually for each university. Now when I update Department1, ofcourse every University which has that school will see changes. I don't want that. I want to update Department1 but only for the University1 record. Department1 in University2 shouldn't be updated.

I assume that adding through option with intermediate model wouldn't help. What's the best solution?

gagro
  • 371
  • 7
  • 18

1 Answers1

0

For the first part of your question

But I also want to store more departments individually for one university.
  • You can crate the departments and only add them to the university you want them to have a relationship with.

For the second part of your question

I want to update Department1 but only for the University1 record. Department1 in University2 shouldn't be updated

  • You should add through tables. A through table defines the relationship between two models and it's purpose is to store what is unique to each specific relationship.

So if you want to update Department 1 for all universities you update Department 1's instance.

If you want to only update Department 1 with something unique between Department 1 and University 1, that goes in the through table between the two models.

enter image description here

JHRS
  • 367
  • 4
  • 11
  • So you're saying I should create new table each time there's relationship between University and Department? Makes no sense! What if there's 1000 relationships, it would create 1000 tables. – gagro Dec 09 '18 at 01:20
  • A through table is a new table in the db. There is only one relationship between a single university and a single department you put ALL that is unique to that relationship in that one table (the through table). Django creates the table for you. – JHRS Dec 09 '18 at 03:29
  • Yes, I know that. I've tried that in first place, but that's not solution because those table still has reference to university and school. Through table stores 2 foreign keys, one from university, one from school. So if there's many rows with same foreign key to school every time I update that school it will reflect on any row, in other words it will reflect on any university. – gagro Dec 09 '18 at 08:00
  • Yes if you update the university itself, that update will be ubiquitous. But if you only update the through table for some property that is unique for the combination of that university and the department, the update will only live in that through table. – JHRS Dec 10 '18 at 03:07