内联管理员表单中的自定义字段视图

时间:2022-03-29 15:57:31

I am using inline model admin and many to many relation. I have two model classes User and Videodata and i linked them with many to many relation. Below is my models

我使用内联模型管理员和许多关系。我有两个模型类User和Videodata,我将它们与多对多的关系联系起来。以下是我的模特

class User(models.Model):
    user_id = models.CharField(max_length=40,unique=True)
    user_name = models.CharField(max_length=40)
    user_email = models.EmailField()
    user_city = models.CharField(max_length=40)
    videos_watched = models.ManyToManyField('VideoData', through='WatchedVideo')
    class Meta:
        ordering = ['user_id']
        verbose_name = 'User MetaData'
        verbose_name_plural = 'Users MetaData'
    def __unicode__(self):
        return self.user_id

class VideoData(models.Model):
    video_id = models.CharField(max_length=40,unique=True)
    video_name = models.CharField(max_length=40) 
    class Meta:
        verbose_name = 'User_Video MetaData'
        verbose_name_plural = 'Users_Video MetaData'
    def __unicode__(self):
        return self.video_id

class WatchedVideo(models.Model):
    user = models.ForeignKey(User, to_field = 'user_id')
    videoData = models.ForeignKey(VideoData, to_field = 'video_id')
    time  = models.PositiveIntegerField(null = True)

In User table i see video data but with video_id but i want video_name to be displayed there. I am attaching a link of current output.

在用户表中,我看到视频数据,但有video_id,但我想在那里显示video_name。我附加了当前输出的链接。

https://drive.google.com/open?id=0B0JOKpp3abpzOFN3LVg0WEs3a0k

https://drive.google.com/open?id=0B0JOKpp3abpzOFN3LVg0WEs3a0k

You can see in above pic video id is there but i want video name. How can i do that??

你可以在上面看到视频ID是有,但我想要视频名称。我怎样才能做到这一点??

2 个解决方案

#1


1  

You can display the name changing the method __unicode__ of VideoData to:

您可以显示将VideoData的方法__unicode__更改为的名称:

def __unicode__(self):
    return self.video_name

EDIT:
Add method __str__ to VideoData:

编辑:将方法__str__添加到VideoData:

def __unicode__(self):
    return self.video_id

def __str__(self):
    return self.video_name

#2


0  

I displayed names with following change:

我显示的名称有以下变化:

def __unicode__(self):
    return self.video_name

def __str__(self):
    return self.video_id

#1


1  

You can display the name changing the method __unicode__ of VideoData to:

您可以显示将VideoData的方法__unicode__更改为的名称:

def __unicode__(self):
    return self.video_name

EDIT:
Add method __str__ to VideoData:

编辑:将方法__str__添加到VideoData:

def __unicode__(self):
    return self.video_id

def __str__(self):
    return self.video_name

#2


0  

I displayed names with following change:

我显示的名称有以下变化:

def __unicode__(self):
    return self.video_name

def __str__(self):
    return self.video_id