Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3437832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:06:48+00:00 2026-05-18T08:06:48+00:00

I have a class Book : from django.db import models from users.models import User

  • 0

I have a class Book:

from django.db import models
from users.models import User

class Book(models.Model):
    title = models.CharField(max_length=150)
    authors = models.ManyToManyField("Author")

which links to the Author class:

class Author(models.Model):
    name = models.CharField(max_length=150)
    birthday = models.DateField()

And I have a form for the Book class called BookForm

from django import forms
from django.forms import CharField
from books.models import Book
class BookForm(forms.ModelForm):
    authors = CharField()
    class Meta:
        model = Book
        fields = ["title","authors"]

I want to create Books with the BookForm which is simple enough, but instead of picking the authors from a list like would happen with the default (since it is a ManyToManyField), I want the user to be able to enter text, a comma separated string of each author’s name. My view is set up like so:

def create_book(request):
    if request.user is None:
        return redirect("/users/login/")
    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            nbook = form.save(commit=False)
            authorlist = form.authors.split(",")
            nbook.owner = request.user
            nbook.save()
            for auth in authorlist:
                nbook.authors.create(name=auth)
            return redirect("/books/")
    else:
        form = BookForm()
    return render_to_response("books/create.html", {
        "form": form,
    }, context_instance=RequestContext(request))

But this does not work. I get the error 'BookForm' object has no attribute 'authors'. I’m not sure if there is a fundamental problem here or if its just syntax, but can someone please lend a hand? I am completely new to both Python and Django :[

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:06:48+00:00Added an answer on May 18, 2026 at 8:06 am

    To stop django automaticly creating the <select> form entry, remove the 'authors' from the fields list:

    class BookForm(forms.ModelForm):
        authors = CharField()
        class Meta:
            model = Book
            fields = ["title"]
    

    Django was overriding the authors entry that you created with the one it automatically created.

    As for your second error, the correct way to get data from a bound form would not be via form.authors, but from form.cleaned_data['authors'].

    P.S. You start with this:

    if request.user is None:
        return redirect("/users/login/")
    

    For this you should use the login_required decorator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have the following models: class Author(models.Model): author_name = models.CharField() class Book(models.Model): book_name =
Lets say i have a class like this class book { public $title; public
I came across this class while reading a C# book and have some questions.
I have class A: public class ClassA<T> Class B derives from A: public class
I am working through the examples in a Django book that I have, but
I have a UI-dialog something like this: You must choose a book from a
As we know in C++ we have class iostream, which is inherited from istream(basic_istream)
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.