简体   繁体   中英

How to set up initial value in Django form for ForeignKey field?

I have problem with setting up initial value for ForeignKey field. I have a CreateView form and I am accessing it from book detail view which has url : path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'), I want to have initial value of this book for example /book/4 in my form already when i enter CreateView

views.py

class BookInstanceCreate(PermissionRequiredMixin, CreateView):
    model = BookInstance
    fields = '__all__'
    permission_required = 'catalog.can_mark_returned'
    initial = {'book': BookInstance.book.id}
    def get_success_url(self):
        return reverse('book-detail', kwargs={'pk': self.object.book.pk})

I was trying to access a book with : initial = {'book': BookInstance.book.id} but i got error: initial = {'book': BookInstance.book.id} AttributeError: 'ForwardManyToOneDescriptor' object has no attribute 'id'

Here are my models.py:

    class Book(models.Model):
        """Model representing a book (but not a specific copy of a book)."""
        title = models.CharField(max_length=200)
        author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)

        # Foreign Key used because book can only have one author, but authors can have multiple books
        # Author as a string rather than object because it hasn't been declared yet in the file.
        summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
        isbn = models.CharField('ISBN', max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')

        # ManyToManyField used because genre can contain many books. Books can cover many genres.
        # Genre class has already been defined so we can specify the object above.
        genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
        language = models.ForeignKey(Language, on_delete=models.SET_NULL, null=True)

        def __str__(self):
            """String for representing the Model object."""
            return self.title

        def get_absolute_url(self):
            """Returns the url to access a detail record for this book."""
            return reverse('book-detail', args=[str(self.id)])

        def display_genre(self):
            """Create a string for the Genre. This is required to display genre in Admin."""
            return ', '.join(genre.name for genre in self.genre.all()[:3])

        display_genre.short_description = 'Genre'

class BookInstance(models.Model):
    """Model representing a specific copy of a book (i.e. that can be borrowed from the library)."""
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular book across whole library')
    book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True) 
    imprint = models.CharField(max_length=200)
    due_back = models.DateField(null=True, blank=True)
    borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    LOAN_STATUS = (
        ('m', 'Maintenance'),
        ('o', 'On loan'),
        ('a', 'Available'),
        ('r', 'Reserved'),
    )

    status = models.CharField(
        max_length=1,
        choices=LOAN_STATUS,
        blank=True,
        default='m',
        help_text='Book availability',
    )
class BookInstanceCreate(PermissionRequiredMixin, CreateView):
     model = BookInstance
     fields = '__all__'
     ermission_required = 'catalog.can_mark_returned'
     def get_success_url(self):
        return reverse('book-detail', kwargs={'pk': self.object.book.pk})

You can't access the value from the class name

initial = {'book': BookInstance.book.id}

It should be the object of that class.

Lets have a closer look at BookInstance.book.id .

BookInstance -> A model class

book -> An unpopulated field in that model ( ForwardManyToOneDescriptor , with emphasis on Descriptor )

id -> A field that django internally populates from the database when we use something like Book.objects.first().id

Therefore, you're trying to get a book foreign key from a model class not a model instance .

You'll probably want to use something similar to set initial value in CreateView from ForeignKey (non-self.request.user) here. You can access the request object from within the get_initial method.

So here's what it might look like:

class BookInstanceCreate(PermissionRequiredMixin, CreateView):
    model = BookInstance
    fields = '__all__'
    permission_required = 'catalog.can_mark_returned'

    def get_initial(self):
        # You could even get the Book model using Book.objects.get here!
        return {
            'book': self.kwargs["pk"]
        }

    def get_success_url(self):
        return reverse('book-detail', kwargs={'pk': self.object.book.pk})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM