Q:

One to Many relationship of models in Django

I have two models Users and Video. I want to link them. I am trying to make relation b/w users and videos they watched. Let us say user1 watched video1,video2 and video3. I want to see all the videos record user1 watched in one table. I defined below model but not working for me...
class User(models.Model):
    user_id = models.IntegerField()
    user_name = models.CharField(max_length=40)
    email = models.EmailField()
    city = models.CharField(max_length=40)
    videoData = models.ForeignKey('VideoData', null = True)
    class Meta:
        ordering = ['user_id']
        verbose_name = 'User MetaData'
        verbose_name_plural = 'Users MetaData'
    def __unicode__(self):
        return str(self.user_id)

class VideoData(models.Model):
    video = models.CharField(max_length=40)
    time  = models.IntegerField()
    class Meta:
        verbose_name = 'User_Video MetaData'
        verbose_name_plural = 'Users_Video MetaData'
    def __unicode__(self):
        return str(self.video)

Here I am able to link one video to user1 but not many videos to user one. When i open user table i see only one video linked but i want to see all video there which user watched. How can i do that??

A:

You need to use ManyToMany relationship here. One user can watch many videos. And one video can be watched by many users. 
Here's an example model structure where you can store more MetaData about the videos the user watched.
class User(models.Model):
    name = models.CharField(max_length=40)
    videos_watched = models.ManyToManyField('Video', through='WatchedVideo')

class Video(models.Model):
    name = models.CharField(max_length=40)
    time  = models.IntegerField()

class WatchedVideo(models.Model):
    user = models.ForeignKey(User)
    video = models.ForeignKey(Video)
    watched_on = models.DateTimeField(auto_now_add=True)

To WatchedVideo inline inside tha UserAdmin, try this:
#admin.py inside your graph app
class WatchedVideoInline(admin.TabularInline):
    model = WatchedVideo

class UserAdmin(admin.ModelAdmin):
    inlines = [WatchedVideoInline]

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

