from django.db import models from simple_history.models import HistoricalRecords class Team(models.Model): """ Team model with canonical identifiers. """ id = models.CharField( max_length=50, primary_key=True, help_text='Canonical ID (e.g., team_nba_lal)' ) sport = models.ForeignKey( 'core.Sport', on_delete=models.CASCADE, related_name='teams' ) division = models.ForeignKey( 'core.Division', on_delete=models.SET_NULL, null=True, blank=True, related_name='teams' ) city = models.CharField( max_length=100, help_text='Team city (e.g., Los Angeles)' ) name = models.CharField( max_length=100, help_text='Team name (e.g., Lakers)' ) full_name = models.CharField( max_length=200, help_text='Full team name (e.g., Los Angeles Lakers)' ) abbreviation = models.CharField( max_length=10, help_text='Team abbreviation (e.g., LAL)' ) home_stadium = models.ForeignKey( 'core.Stadium', on_delete=models.SET_NULL, null=True, blank=True, related_name='home_teams' ) primary_color = models.CharField( max_length=7, blank=True, help_text='Primary color hex (e.g., #552583)' ) secondary_color = models.CharField( max_length=7, blank=True, help_text='Secondary color hex (e.g., #FDB927)' ) logo_url = models.URLField( blank=True, help_text='URL to team logo' ) is_active = models.BooleanField( default=True, help_text='Whether team is currently active' ) # Metadata created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) # Audit trail history = HistoricalRecords() class Meta: ordering = ['sport', 'city', 'name'] verbose_name = 'Team' verbose_name_plural = 'Teams' def __str__(self): return self.full_name @property def conference(self): """Return team's conference via division.""" if self.division: return self.division.conference return None