Django数据库自学

发布于 2024-09-29 16:25:36 字数 1063 浏览 5 评论 0原文

我有一个依赖其他模型的系统,但希望使其更加开放,因为我的用户有时需要更多一点。

目前,为了论证,我在一个 Author 模型中有 2 位作者

john, james

当用户添加一本书,并且 Author 不可用时,如何在表单中添加 other 字段,如果选择该字段,则会生成一个额外的字段,其中用户可以输入作者的姓名,并且表单处理的方式是在提交时添加并选择新作者?

MODELS.PY

class Author(models.Model):
    name = models.CharField(max_length=30)

FORMS.PY

class AuthorForm(ModelForm):
    class Meta:
        model = Author

VIEWS.PY

def new(request, template_name='authors/new.html'):

if request.method == 'POST':
    form = AuthorForm(request.POST, request.FILES)
    if form.is_valid():
        newform = form.save(commit=False)
        newform.user = request.user
        newform.save()

        return HttpResponseRedirect('/')

else:
    form = AuthorForm()

context = { 'form':form, }

return render_to_response(template_name, context,
    context_instance=RequestContext(request))

I have a system that relies on other models but want to make it a little more open-ended as my users demand a bit more sometimes.

currently for arguments sake I have 2 authors in an Author model.

john, james

When a user adds a book, and the Author is not available, how do I add a other field in the form, that if selected, then generates an extra field where the user can enter the name of the Author and the form handling is in such a way that the new Author gets added and selected on submission?

MODELS.PY

class Author(models.Model):
    name = models.CharField(max_length=30)

FORMS.PY

class AuthorForm(ModelForm):
    class Meta:
        model = Author

VIEWS.PY

def new(request, template_name='authors/new.html'):

if request.method == 'POST':
    form = AuthorForm(request.POST, request.FILES)
    if form.is_valid():
        newform = form.save(commit=False)
        newform.user = request.user
        newform.save()

        return HttpResponseRedirect('/')

else:
    form = AuthorForm()

context = { 'form':form, }

return render_to_response(template_name, context,
    context_instance=RequestContext(request))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-10-06 16:25:36

假设您的书籍模型是这样的:

class Book(models.Model):
    author = models.ForeignKey(Author)
    #etc.

尝试这样的事情:

class BookForm(ModelForm):
    other = forms.CharField(max_length=200, required=False)

    def clean(self, *args, **kwargs):
        other = self.cleaned_data.get('other')
        author = self.cleaned_data.get('author')
        if author is None or author == '': 
            self.cleaned_data['author'] = Author(name=other).save()

        return self.cleaned_data

    class Meta:
        model = Book

如果作者是一个manytomanyfield:

class BookForm(ModelForm):
其他 = forms.CharField(max_length=200, required=False)

def clean(self, *args, **kwargs):
    other = self.cleaned_data.get('other')
    author = self.cleaned_data.get('author')
    if other is not None or other != '':
        for newauthor in other.split(','): #separate new authors by comma. 
            self.cleaned_data['author'].append(Author(name=newauthor).save())

    return self.cleaned_data

class Meta:
    model = Book

assuming your book model is this:

class Book(models.Model):
    author = models.ForeignKey(Author)
    #etc.

try something like this:

class BookForm(ModelForm):
    other = forms.CharField(max_length=200, required=False)

    def clean(self, *args, **kwargs):
        other = self.cleaned_data.get('other')
        author = self.cleaned_data.get('author')
        if author is None or author == '': 
            self.cleaned_data['author'] = Author(name=other).save()

        return self.cleaned_data

    class Meta:
        model = Book

If author would be a manytomanyfield:

class BookForm(ModelForm):
other = forms.CharField(max_length=200, required=False)

def clean(self, *args, **kwargs):
    other = self.cleaned_data.get('other')
    author = self.cleaned_data.get('author')
    if other is not None or other != '':
        for newauthor in other.split(','): #separate new authors by comma. 
            self.cleaned_data['author'].append(Author(name=newauthor).save())

    return self.cleaned_data

class Meta:
    model = Book
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文