0

I have a model which contains a user, each user is associated to a club, a club can contain many teams, pitches etc. I want to know how I should design my models so that I can display/edit information on teams/pitches based on the user logged in and the club associated to that user. My ClubInfo model contains a foreign key associated to the user, where as my other models (Team/Pitch) have foreign keys associated to the ClubInfo not the user.

class ClubInfo(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
club_name = models.CharField(max_length=50, default='')
club_logo = models.ImageField(upload_to='profile_pics', blank=True)
club_address1 = models.CharField(max_length=30)
club_address2 = models.CharField(max_length=30, default='')
club_address3 = models.CharField(max_length=30, default='')
club_town = models.CharField(max_length=30)
club_county = models.CharField(max_length=30)
club_country = models.CharField(max_length=30)

def __str__(self):
    return self.club_name



class Player(models.Model):

club_name = models.ForeignKey(ClubInfo, to_field='id', on_delete=models.CASCADE, unique=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
dob = models.DateField(max_length=8)
email = models.EmailField(max_length=50)
phone = models.CharField(max_length=12)
mobile = models.CharField(max_length=15)
emergency_contact_name = models.CharField(max_length=40)
emergency_contact_mobile = models.CharField(max_length=15)
address1 = models.CharField(max_length=30)
address2 = models.CharField(max_length=30, default='')
address3 = models.CharField(max_length=30, default='')
town = models.CharField(max_length=30)
county = models.CharField(max_length=30)
country = models.CharField(max_length=30)

def __str__(self):
    return "%s %s" % (self.first_name, self.last_name)


class Team(models.Model):

club_name = models.ForeignKey(ClubInfo, to_field='id', on_delete=models.CASCADE, unique=True)
team_name = models.CharField(max_length=30)
manager_name = models.CharField(max_length=20)
player_pk = models.ForeignKey(Player, to_field='id', on_delete=models.CASCADE, unique=True)

def __str__(self):
    return self.team_name


class Pitch(models.Model):
club_name = models.ForeignKey(ClubInfo, to_field='id', on_delete=models.CASCADE, unique=True)
pitch_name = models.CharField(max_length=30)
PITCH_SIZES = (
    ('S', 'Small'),
    ('M', 'Medium'),
    ('L', 'Large'),
)
PITCH_TYPE = (
    ('1', 'Outdoor'),
    ('2', 'Indoor'),
)
pitch_size = models.CharField(max_length=1, choices=PITCH_SIZES)
pitch_type = models.CharField(max_length=1, choices=PITCH_TYPE)
open_time = models.TimeField(default='09:00')
close_time = models.TimeField(default='22:00')

def __str__(self):
    return self.pitch_name
user9629702
  • 55
  • 1
  • 2
  • 9

2 Answers2

0
  1. club can contain many teams, pitches etc.

Then you probably don't want to use unique=True in your foreign keys, since the behaviour is quite similar to OneToOneField. That would mean that each Club can only have one player, one pitch etc.

Please refer to: What's the difference between django OneToOneField and ForeignKey?

  1. You don't need to write explicitly to_field='id', it's the default value for a to_field argument

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.to_field

  1. so that I can display/edit information on teams/pitches based on the user logged

You need to define reverse relation: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name

For example:

class Pitch(models.Model):
    club_name = models.ForeignKey(ClubInfo, on_delete=models.CASCADE, related_name="pitches")
    (...)

Then you can query for player's pitches in the following way:

player = Player.objects.get(<get your player>).club_name.pitches
  1. In your current design (after removing a unique=True) a team can consist of only one player, and players can be in many teams. Is that expected? Most likely you would like to have

1) Team with many players - then you would need to add foreign key to Team in a Player model

2) Team with many players, but also players can be in many teams - then you would like to have a many-to-many relationship: https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

  1. club_name as a foreign key is a bad naming convention - it does not point to club_name, but to Club object
Tomatosoup
  • 106
  • 2
  • 7
  • Thanks so much for the feedback I will take a look at the documentation you've linked. It does seem to clear up a lot of things appreciate it. Thanks Gavin – user9629702 Jan 05 '19 at 16:34
0

club = ClubInfo.objects.filter()

blog = Player.objects.filter(Q(club_name__in=club))