在调用python对象时,超过了/形式/电影/最大递归深度



我不确定在django中工作了多个tomany字段。因此,我在这里拥有的是一个配置文件模型,其中我想在"名称"字段中添加我将从登录凭据中获得的用户名,并且将从使用电影模型创建的表单中添加电影。我想在此处了解views.py文件中有什么问题。谢谢输入图像说明

在此处输入图像描述

我的models.py文件:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length = 200)
    body = models.TextField()
    pub_date = models.DateTimeField('date published')
    likes = models.IntegerField(default = 0)
    def __str__(self):
        return self.title

class Comment(models.Model):
    name = models.CharField(max_length = 200)
    body = models.TextField()
    pub_date = models.DateTimeField('date published')
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
class Language(models.Model):
    name = models.CharField(max_length = 20)
    def __str__(self):
        return self.name
class Framework(models.Model):
    name = models.CharField(max_length = 20)
    language = models.ForeignKey(Language, on_delete=models.CASCADE)
    def __str__(self):
        return self.name
class Movie(models.Model):
    name = models.CharField(max_length = 20)
    def __str__(self):
        return self.name
class Character(models.Model):
    name = models.CharField(max_length = 20)
    movies = models.ManyToManyField(Movie)
    def __str__(self):
        return self.name
class profile(models.Model):
    name = models.ForeignKey(User,on_delete=models.CASCADE, related_name="name")
    # name = models.CharField(max_length = 20)
    movies = models.ManyToManyField(Movie)
    def __str__(self):
        return self.movies

我的表格。Py文件:

from django import forms
from . models import Article, Comment, Movie, Character
class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = [
            'title',
            'body',
            'pub_date'
        ]
class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = [
            'name',
            'body'
        ]

类Movieform(forms.modelform):

class Meta:
    model = Movie
    fields = [
        'name'
    ]

class trameform(forms.modelform):

class Meta:
    model = Character
    fields = [
        'name'
    ]

我的views.py文件:

from django.shortcuts import render
from django.contrib.auth.models import User
from .models import Movie, Character, profile
from .forms import MovieForm, CharacterForm
# Create your views here.
def characterset(request):
    # form = CharacterForm(request.POST or None)
    # if form.is_valid():
    #     form.save()
    #     form = CharacterForm()
    form2 = MovieForm(request.POST or None)
    if form2.is_valid():
        form2.save()
        form2 = MovieForm()

    context = {
        'form2' : form2
    }
    return render(request, "manytomany/form2.html", context)
def movieset(request):
    current = request.user.username
    print(current)
    modelname = User.objects.get(username=current)
    print(modelname)
    a = profile(name=modelname)
    print(a)

    form = MovieForm(request.POST or None)
    if form.is_valid():
        f = form.save()
        a.movies.add(f)
        a.save()
    # form2 = MovieForm(request.POST or None)
    # if form2.is_valid():
    #     form2.save()
    #     form2 = MovieForm()

    context = {
        'form' : form
    }
    return render(request, "manytomany/form.html", context)
def show(request, id):
    a = Movie.objects.get(id = id)
    c = Character.objects.get(id = id)
    c.movies.add(a)
    return render(request, "manytomany/show.html")

带有错误的Django页面。它引用了return self.movies这是由于您为该特定模型建立的关系。

所以发生了什么:

个人资料 ->电影 ->多次 ->个人资料

看到这里发生了什么?您要圆圈,并通过"圆形参考"来打破Django。您可以做的是删除有问题的线:

class profile:
    ...
    __str__(self):
        return self.name # or something like it

相关内容

  • 没有找到相关文章

最新更新