0

I have 2 models and a Foreign Key between them

class League(models.Model):
    """
    League Info
    """
    def __unicode__(self):
        return self.name

    FUTBOL = 'FUT'
    FUTBOLITO = 'FT7'
    OTRO = 'OTH'

    LEAGUE_CHOICES = (
        (FUTBOL, 'Fútbol'),
        (FUTBOLITO, 'Futbolito'),
        (OTRO, 'Otro'),
    )

    MASCULINO = 'M'
    FEMENINO = 'F'
    MULTIPLE = 'A'
    SEX_CHOICE = (
        (MASCULINO, 'Masculino'),
        (FEMENINO, 'Femenino'),
        (MULTIPLE, 'Femenino y Masculino'),
    )

    league_type = models.CharField(null=False, blank=False, max_length=3, choices=LEAGUE_CHOICES, default=FUTBOLITO)
    league_sex = models.CharField(max_length=2, choices=SEX_CHOICE, default=MASCULINO)

    name = models.CharField(null=False, blank=False, max_length=200)

The second Model is

class Division(models.Model):
    """
    Division of a League  e.g. Junior, Senior; First Division, Second Division
    """

    def __unicode__(self):
        return self.name


    MASCULINO = 'M'
    FEMENINO = 'F'
    MULTIPLE = 'A'
    SEX_CHOICE = (
        (MASCULINO, 'Masculino'),
        (FEMENINO, 'Femenino'),
        (MULTIPLE, 'Femenino y Masculino'),
    )

    name = models.CharField(null=False, blank=False, max_length=200)  # name required

    league = models.ForeignKey(League, blank=False)
    league_name = league.name
    league_type = league.league_type
    league_sex = league.league_sex

when I run syncdb i got an error : AttributeError: 'ForeignKey' object has no attribute 'league_type'

I got the same error with league_sex, but don't get it with league_name, which seems to be ok. The app is on the setiings Installed Apps

2
  • league_name = league.name this is not the right way to define model fields. What are you trying to do here? Commented Nov 26, 2013 at 13:58
  • I just need some info (fields) from the related model Commented Nov 26, 2013 at 14:00

1 Answer 1

1

You are doing it wrong, just remove following lines from Division model:

league_name = league.name
league_type = league.league_type
league_sex = league.league_sex

Later on if you want to access league info from division object you can do:

division = Division.objects.get(id=some_id)
league = division.league
print league.name
print league.league_type

I think you are just confused.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.