2012-10-24 5 views
0

내 장고 앱에 적합한 모델을 만들려고합니다. 나는 사용자가 그 사용자에게 묶여있는 하나 이상의 (또는 그 이상의) 재생 목록에 URL을 저장할 수있는 무언가를 만들려고 노력하고있다. 이것을 구현하기 전에 이것이 내 models.py를 구조화하는 가장 좋은 방법인지 확인하고자한다.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 
+0

차이 OneToOne http://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey – iMom0

+0

@ iMom0 무엇 다른 질문들에 대해서? 이것이 내가 가능하게하고 싶은 바른 길로 설정되어 있다고 생각합니까? – sharataka

답변

0

우와. 첫째, 질문은 아마 너무 "현지화"되어있을 것이다. 어쨌든. 나는 이런 식으로 할 거라고 : 외래 키 사이

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) 
관련 문제