'ManyRelatedManager'对象没有属性'_meta'[duplicate]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

[当我尝试编辑表单时,发生了以上错误。我试图通过对模型使用“直通”来解决此问题,但这可能不是数据库的正确结构。有人可以帮我吗。

models.py

models.py中的代码

class Singer(models.Model):
    first_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)

class Album(models.Model):
    title = models.CharField(max_length=50)
    discription = models.TextField()
    album_creator = models.ManyToManyField(Singer, related_name='sung_by', blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

class Song(models.Model):
    song_title = models.CharField(max_length=25)
    album = models.ForeignKey(Album, related_name='album_name', on_delete=models.CASCADE, blank=True)
    singer = models.ManyToManyField(Singer, blank=True)
    language = models.CharField(max_length=25)

views.py

视图中的代码。

class AlbumSinger(View):
    singer_class = SingerForm
    album_class = AlbumForm
    song_class = SongForm
    template_name = 'user/profile_edit.html'

    def get(self,request,pk=None):
        if pk:
            album_obj = get_object_or_404(Album, pk=pk)
            singer_obj = album_obj.album_creator
            songs = album_obj.album_name.all()

            singer_form = SingerForm(instance=singer_obj)
            album_form = AlbumForm(instance=album_obj)
            song_form = [SongForm(prefix=str(song.song_title),instance=song) for song in songs]

            context = {
                'singer_form':singer_form,
                'album_form':album_form,
                'song_form':song_form
            }
            return render(request, self.template_name, context)
        else:
            singer_form = self.singer_class(None)
            album_form = self.album_class(None)
            song_form = self.song_class(None)

            context = {
                'singer_form':singer_form,
                'album_form':album_form,
                'song_form':song_form
            }
            return render(request, self.template_name, context)
python django django-models django-rest-framework django-views
1个回答
0
投票

要创建requirements.txt文件,请在虚拟环境中运行以下命令:

pip freeze > requirements.txt

它将创建一个requirements.txt文件,并将所有已安装软件包的列表及其版本打印到requirements.txt文件中。

© www.soinside.com 2019 - 2024. All rights reserved.