1

I have a website built in Django 1.10. The site has 3 different apps: teams, members and news.

The first app, called teams has one model called Team.

This is the Team/models.py:

from django.db import models
from django.db.models.signals import pre_save
from django.utils.text import slugify


class Team(models.Model):
        name = models.CharField(max_length=255)
        description = models.TextField()
        slug = models.CharField(max_length=255, default='team', editable=True)



        class Meta:
                ordering = ('name',)

        def __unicode__(self):
                return self.name

The second app, called members has one model called Member.

This is the Member/models.py:

from django.db import models

class Piloto(models.Model):
        name = models.CharField(max_length=255)
        biography = models.TextField()
        slug = models.CharField(max_length=255, default='piloto', editable=True)

        class Meta:
                ordering = ('name',)

        def __unicode__(self):
                return self.name

What I want is include the name of the team inside the member profile, so I know it should be something like:

team_of_member = models.ForeignKey();

But I don't know what to put in the parenthesis or how to import the model of the team to the model of the member. I was following the documentation of Django 1.10 but it wasn't working, also I've tried this link but it doesn't work. Could you give a hand? Thanks

Edit: I tried to do as @Bulva was suggesting, so my code is now like this:

from django.db import models
from equipos.models import Team

class Member(models.Model):
        name = models.CharField(max_length=255)
        team = models.ForeignKey('teams.Team', null=True)
        biography = models.TextField()
        slug = models.CharField(max_length=255, default='piloto', editable=True)

        class Meta:
                ordering = ('name',)

        def __unicode__(self):
                return self.name
Community
  • 1
  • 1
Albert
  • 167
  • 2
  • 16

3 Answers3

2

Try this:

from teams.models import Team

# in the member model field
team_of_member = models.ForeignKey(Team);
Bulva
  • 1,208
  • 14
  • 28
  • It gives me an error ImportError: cannot import name Equipo – Albert Mar 31 '17 at 13:40
  • Now you should really edit your answer and add your code. How @haken lid said – Bulva Mar 31 '17 at 13:42
  • I added all the code from both models, can you see it? because I pasted all, but I'm not sure that you are able to see it. Thanks a lot – Albert Mar 31 '17 at 13:43
  • I use sublime @Bulva – Albert Mar 31 '17 at 13:44
  • You don't mention `Equipo` in your question. The error message will say in which file and on which line you are attempting to import. – Håken Lid Mar 31 '17 at 13:59
  • 1
    That error is obviously in some other part of your code that you have not included in your question. The traceback of the exception will tell you exactly where in the code the error was raised. You are trying to import some class that doesn't exist, or import it from an incorrect location. – Håken Lid Mar 31 '17 at 14:03
1

In order to do what you want (assuming the code provided is your full "problematic" code) you should:

In Member/models.py:

from django.db import models
from teams.models import Team  # <--add this line

class Piloto(models.Model):
        name = models.CharField(max_length=255)
        team_of_member = models.ForeignKey(Team, on_delete=models.CASCADE); # <--add this line
        biography = models.TextField()
        slug = models.CharField(max_length=255, default='piloto', editable=True)

        class Meta:
                ordering = ('name',)

        def __unicode__(self):
                return self.name

After you do this, remember to:
python manage.py makemigrations
python manage.py migrate

to change your models state.

Good luck :)

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • it worked but putting `team_of_member = models.ForeignKey('teams,Team', on_delete=models.CASCADE);` – Albert Mar 31 '17 at 14:12
  • here http://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models?answertab=votes#tab-top is a very good explanation on what models.CASCADE do! – John Moutafis Mar 31 '17 at 14:13
1

What you want is to provide the teams the member is a member of. It all depends on your business logic. A team has many members by definition, but can a member be a part of many teams? If yes, you have a many-to-many relationship. If no, you have a one-to-many relationship.

Under one-to-many assumption, the foreignkey information has to be put in the referenced model. Then:

from django.db import models
from team.models import Team  # Generally, apps are in all lower-case (assuming your app is called team)

class Member(models.Model):
    name = models.CharField(max_length=255)
    team = models.ForeignKey('team.Team', related_name = 'members', null=True)  # Do not forget to put team.Team inside a pair of single-quotes.
    biography = models.TextField()
    slug = models.CharField(max_length=255, default='piloto', editable=True)

    class Meta:
            ordering = ('name',)

    def __unicode__(self):  # use def __str__(self): in Python 3+
            return self.name

In your view, you can then say this:

albert_team = albert.team  
albert_teammates = albert_team.members

Under Many-to-Many assumption, it is more natural to capture the relationship in the team model:

from django.db import models
from django.db.models.signals import pre_save
from django.utils.text import slugify


class Team(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.CharField(max_length=255, default='team', editable=True)
    members = models.ManyToManyField('team.Member', related_name = 'teams')

    class Meta:
            ordering = ('name',)

    def __unicode__(self):
            return self.name

In views.py:

albert_teams = albert.teams

all_albert_teammates = []
for team in albert_teams:
    all_albert_teammates.append(team.members)
EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • Thank you so much, that was exactly what I was looking for! I really appreciate it. – Albert Apr 04 '17 at 05:34
  • I'm in the case of that a member can only be part of one team. The only thing is that in the views.py I'm using class based views, so do I need to do anything in that file? And also, one last thing if you don't mind: in my teams model if I want to show the members that it has, how I can do it?Thank you – Albert Apr 04 '17 at 07:56