u/AyrtonHS

▲ 3 r/django

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.

reddit.com
u/AyrtonHS — 1 day ago
▲ 3 r/django

A team has 2 accounts. We represent these 2 accounts with the same model. Both of these accounts should be used by only one team. I used forms.OneToOneFieldin an attempt to apply the one-account restriction. It only partially works. Django allows team 1 to use account a as its first account and team 2 to use the same account a as its second account. I wonder if anyone has encountered something similar and is able to apply a stricter constraint where an account can only be used a single time.

reddit.com
u/AyrtonHS — 15 days ago
▲ 8 r/django

I was just wondering why Rest Framework has its own Authentication and Permission features. Do we have to use these when working with Rest Framework?

reddit.com
u/AyrtonHS — 18 days ago