Would a one-to-many field make things easier?
Disclaimer: I already found a way to make the database structure work, I just curious about this concept and if it would make sense to exist.
So in our project, every Team of people has an Account that stores their points. Originally, every team has one account and vice versa, so I just used a OneToOne Field. For convenience, Accounts can be automatically created when a Team is created if the admin doesn't make an account in advance. Originally there was no problem. The Team Model file imports the Account Model to both u
In Team's model (simplified for privacy issues):
import Account
account = models.OneToOneField(Account)
def save(self, *args, **kwargs):
account = Account(name=self.name + " points")
account.save()
self.account = account
Meanwhile, Account never imports Team, so things were fine.
However, I reread the requirements and noticed that the client wants each team to have multiple accounts, but each account can only belong to one team.
Since one-to-many doesn't really exist, I assume the best way is to define a ForeignKey in Account to point to Team:
import Team
team = models.ForeignKey(Team)
Here's the problem. We still import Account in Team in order to create the accounts for the teams, since the teams need the accounts (it needs at least one, and it is required to have a certain number of accounts based on conditions). This leads to circular import.
Now the problem has already been fixed using Lazy relationships, but I wonder: if there was a one-to-many field, would I be able to connect to Account in Team and therefore only import Account in Team and not vice versa? This is embarrassing, but I first asked Chatgpt and it kept telling me that it's not how it works. Thank you.