0

I'm trying to build the right models for my Django app. I'm trying to build something that will allow a user to save a URL into one (or more) playlist(s) that is tied to that user. Before I implement this, I want to make sure that this is the best way to structure my models.py.

class UserProfile(models.Model):
    user = models.ForeignKey(User, primary_key=True)    #what is the difference between ForeignKey and OneToOne?  Which one should I use?
    Playlist = models.CharField('Playlist', max_length = 2000)  #1 user should be able to have multiple playlists and the default playlist should be "Favorites"
    def __unicode__(self):
        return self.User

class Videos(models.Model):
    Video_url = models.URLField('Link to video', max_length = 200, null=True, blank=True)
    Playlist = models.ManyToManyField(Playlist) #this should connect to the playlists a user has.  A user should be able to save any video to any plalist, so perhaps this should be ManyToMany?
    def __unicode__(self):
        return self.Video_url
sharataka
  • 5,014
  • 20
  • 65
  • 125
  • difference between ForeignKey and OneToOne http://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey – iMom0 Oct 24 '12 at 01:05
  • @iMom0 what about the other questions? Do you think this is set up the right way for what I'd like to enable? – sharataka Oct 24 '12 at 03:12

1 Answers1

0

Woah. Firstly the question is probably too "localised" for SO. Anyway. I'd do it like this:

class PlayList(models.Model):
    playlist = models.CharField(max_length=2000)

class UserProfile(models.Model):
    # do you want each `User` to only have one `UserProfile`? If so then OneToOne
    # primary keys are automatically formed by django
    # how django handles profiles: https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
    user = models.ForeignKey(User)

    def __unicode__(self):
        return self.User

class UserPlayList(models.Model):
    # don't capitalise attributes, if you haven't seen PEP8 before, do now: http://www.python.org/dev/peps/pep-0008/
    profile = models.ForeignKey(User)
    playlist = models.ForeignKey(PlayList)

class Video(models.Model):
    video_url = models.URLField(max_length=200, null=True, blank=True, help_text="Link to video")

    def __unicode__(self):
        return self.video_url

class VideoPlayList(models.Model):
    video = models.ForeignKey(Video)
    play_list = models.ForeignKey(UserPlayList)
Williams
  • 4,044
  • 1
  • 37
  • 53