调用Python对象时,/ form / movie /的最大递归深度超过RecursionError

时间:2019-03-08 19:42:32

标签: python django orm django-forms manytomanyfield

我不太确定Django有多少个领域。所以我这里是一个配置文件模型,其中我想在名称字段中添加我将从登录凭据中获取的用户名,并且将从使用电影模型创建的表单中添加电影。我想了解此处的views.py文件出了什么问题。谢谢enter image description here

enter image description here

我的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

我的forms.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'
    ]

CharacterForm(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")

1 个答案:

答案 0 :(得分:0)

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

这是怎么回事:

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

看看这里发生了什么?您正在绕圈而行,并通过“圆形参考”破坏了Django。您可以做的是删除有问题的行:

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