It seems like a trivial question but I am new to Django. I have two models user and competition.
The user can create and join one or many competition.
How to specify this relation as the user can be the owner of competition/s and can be a participant in one or more competition.
I Assume You have two tables User and Competition:
then in competition you can user models.ManyToManyField
Example
class User:
...
class Competition:
...
creator = models.ForeignKey(User)
participents = models.ManyToManyField(User)
Related
I have a Netflix clone app that I am creating and I am having difficulty with the models.
I have a Movie model that has fields for the movie such as a title, duration, rating, etc.
I am not sure how I should model my database but what I believe I should be doing is a ManyToMany relationship, such as, a user can favorite many movies and a movie can be favorited by many users.
However, if I do it like this I feel that I would end up with a Favorites table that would have many users and movies and possible see this as an issue having to go through each row to find the current user rather than doing maybe a OneToMany relationship, such as, a user can favorite many movies and a movie can be favorited by a user.
So first, I am not sure what the proper way of modeling this would be.
I also do not know how to add this relationship to my user model because I am using the User model that I brought in from django.contrib.auth.models
I am having a lot of trouble trying to think of a solution for this and I don't want to just add a many to many field for users to the movie table that I have without understanding if and why it is a good/bad approach.
How about something like:
class User(...):
first_name = models.CharField(...)
....
class Movie(models.Model):
title = models.CharField(...)
...
class Favorite(models.Model):
user = models.ForeignKey('User', related_name='favorites', ...)
movie = models.ForeignKey('Movie', related_name='favorites', ...)
Then we can do:
# a particular user's favorite movies:
user = User.objects.get(id='the_user_id')
user.favorites.values('movie')
# number of users who have favorited a particular movie:
movie = Movie.objects.get(id='the_movie_id')
movie.favorites.count()
I'm am building a django app which takes user Interests as inputs.
Now I have 2 Questions -
First is that, what model should I use, should I just add a field to user model or a separate Interest Model and link via Foreign Key?
I know the former design is bad, and so I.m trying latter one, I'm having a hard time in Django to create Interest Model and its view to save the user interests.
Any help is appreciated.
I am trying to accomplish the same thing.
Here is how I have solved it:
I have not tried it out yet, but this should work as a solution.
from django.contrib.auth.models import User
class Nation(models.Model):
name=models.CharField(max_length=64)
class Subject(models.Model):
name=models.CharField(max_length=64)
class Interests(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nationals = models.ManyToManyField(Nation)
subjects = models.ManyToManyField(Subject)
In Django model I am making a table 'followers', which has:
user's id. (this is followed by)
user's id (this is follower)
that's simple a user can follow other users.
How should I define the model in Django?
I tried this, but does not work:
user = models.ForeignKey('self')
follower_id = models.ForeignKey('self')
How should this be done?
thanks
The 'self' argument won't work unless you have a model called self.
Assuming that your assignment model is called Following, and you're using the built in User model then you can do:
class Following(models.Model):
target = models.ForeignKey('User', related_name='followers')
follower = models.ForeignKey('User', related_name='targets')
This will likely need some further uniqueness and validation logic.
Note the related_name attribute, see https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_name. This means that for a given user object you can do user.targets.all() to get users they follow, and user.followers.all() to get users who follow them.
Note also that Django returns target model instances, not IDs, in the ORM. This means that even though the underlying table may be called follower_id, in the python code following.follower will return an actual User object.
Seeing as Following is actually the through table for the many-to-many relationship between Users. I would create a Profile model which extends the Django User model, and then declare the many-to-many relationship (using ManyToManyField).
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
following = models.ManyToManyField(User, related_name='followers')
Use the many to many field.
followers = models.ManyToManyField('self', symmetrical=False)
from django.contrib.auth.models import User
class UserType1(models.Model):
user = models.OneToOneField(User,parent_link=True,primary_key=True)
class UserType2(models.Model):
user = models.OneToOneField(User,parent_link=True,primary_key=True)
class UserType3(models.Model):
user = models.OneToOneField(User,parent_link=True,primary_key=True)
Each of three types of users has its own unique fields.
I want to let users be able to follow any user he/she wants.
How can I do a manytomany among three different model classes?
Is there a better way of doing this? Maybe an abstract model between Model.auth.User and the three types of users?
Just create a ManyToManyField in each model point to User
class UserType1(models.Model):
user = models.OneToOneField(User, parent_link=True,primary_key=True)
followed_users = models.ManyToManyField(User, related_name='followers')
Do the same with other models. Better yet, create an abstract base class for the common fields in your models. This will save you time and trouble of typing common fields again and again.
I intend to create a teaming system, where each team may contain multiple contestants. The contestants is actually auth.User. Something similar to:
Team:
Contestant1
Contestant2
.
.
ContestantN
Since a contestant is actually a user which I cannot modify to have a foreignkey to team. What is the best way to achieve this?
The ways I though was:
Create a OneToOne profile for user which points to a team.
Define a ManyToMany relationship between user and team where user has to be unique.
A Pause
I am redesigning the structure of my application, so I will rephrase the question again
Thanks for your replies, I will consider them and see if one of them fits.
You can do this:
class Team(models.Model):
contestants = models.ManyToManyField(User, through='Contestant')
class Contestant(models.Model):
team = models.ForeignKey(Team)
user = models.ForeignKey(User)
[here go Contestant data fields]
This allows one user to take part in different teams, but if you don't want to allow this, you can add unique=True to Contestant.user.
The best way would be to extend the functionality of default accounts and create a new user model. The new user model can then have a foreign key to team. Like this.
class UserExtended(models.Model):
def __unicode__(self):
return self.user.username
user = models.OneToOneField(User, unique=True)
team = models.ForeignKey(Team)
User.profile = property(lambda u: UserExtended.objects.get_or_create(user=u)[0])
Now you can use the "UserExtended" in place of normal User.
I would create a contestants field on the Team model like so:
from django.contrib.auth.models import User
contestants = models.ManyToManyField(User)
You can't specify unique=True on a ManyToManyField. The good news is that it won't add the same contestant to the same team twice so you won't need to check if the contestant is unique.
I would say your best bet is to create a Contestant model. You'll probably end up needing to store more information about a contestant that is team-specific but separate from a player (such as whether the contestant is a starter, the contestant's number, and so on). Creating a Contestant model allows you to store that information separate from the User, and you would have a ForeignKey in the Contestant model referencing Users, and another ForeignKey in the Contestant model referencing Teams.