django管理多对多的中间模型添加了新的

时间:2022-10-04 16:05:49

I have the following models (simplified):

我有以下模型(简化):

class Concert(models.Model):
    title = models.CharField(max_length=50) 
    date = models.DateField()
    songs = models.ManyToManyField(Song,
                                   through='RunOrder',
                                   related_name='run_order',
                                   blank=True)

class Song(models.Model):
    title = models.CharField(max_length=50)


class RunOrder(models.Model):
    concert = models.ForeignKey(Concert, on_delete=models.CASCADE)
    song = models.ForeignKey(Song, on_delete=models.CASCADE)
    act_no = models.SmallIntegerField()
    scene_no = models.SmallIntegerField()

Basically, songs can be in multiple concerts, concerts have multiple songs.

基本上,歌曲可以在多场音乐会,音乐会有多种歌曲。

While creating a new concert in the admin view, I would like to be able to add new songs. Here's what I have:

在admin视图中创建新演唱会时,我希望能够添加新歌曲。这就是我有:

class ConcertAdmin(admin.ModelAdmin):
    ...
    inlines = [SongInline]

class SongInline(admin.TabularInline):
    model = RunOrder
    show_change_link = True
    extra = 1

But this only lets me select from existing songs. In order to add a new song, I have to use the Song admin interface. When I tried to use Song as the model for SongInline, I got a has no ForeignKey error. Is there a way I can streamline/inline the process of adding new Songs to a Concert?

但这只能让我从现有的歌曲中进行选择。为了添加一首新歌,我必须使用歌曲管理界面。当我尝试用歌曲作为歌曲的模型时,我得到了一个没有外来的错误。是否有一种方法可以使我在音乐会中添加新歌曲的过程流线化?

1 个解决方案

#1


1  

It turns out this is a default feature, but the admin interface has to be enabled for the Song model:

这是一个默认的特性,但是管理界面必须为歌曲模型启用:

admin.site.register(Song)

Then you get the little plus sign. Screenshot of django admin inline table.

然后你得到一个加号。django管理内联表的屏幕截图。

#1


1  

It turns out this is a default feature, but the admin interface has to be enabled for the Song model:

这是一个默认的特性,但是管理界面必须为歌曲模型启用:

admin.site.register(Song)

Then you get the little plus sign. Screenshot of django admin inline table.

然后你得到一个加号。django管理内联表的屏幕截图。