我在为ForeignKey字段设置初始值时遇到问题。我有一个CreateView
表单,并且正在从具有url的书详细信息视图访问它:path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'),
我想在输入时已经在表单中拥有这本书的初始值,例如/book/4
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})
我尝试使用initial = {'book': BookInstance.book.id}
访问一本书,但出现错误:initial = {'book':BookInstance.book.id}
AttributeError:“ ForwardManyToOneDescriptor”对象没有属性“ id”
这是我的模特。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',
)
答案 0 :(得分:0)
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})
您不能从类名访问值
initial = {'book': BookInstance.book.id}
它应该是该类的对象。
答案 1 :(得分:0)
让我们仔细看看BookInstance.book.id
。
BookInstance
->模型类
book
->该模型中未填充的字段(ForwardManyToOneDescriptor
,重点是Descriptor
)
id
->当我们使用类似Book.objects.first().id
因此,您正在尝试从模型class
中获取图书外键。不是模型instance
。
您可能要在此处使用类似于set initial value in CreateView from ForeignKey (non-self.request.user)的名称。您可以从get_initial
方法中访问请求对象。
所以可能是这样:
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})