【问题标题】:How to set up initial value in Django form for ForeignKey field?如何在 Django 表单中为 ForeignKey 字段设置初始值?
【发布时间】:2019-03-20 18:25:38
【问题描述】:

我在为 ForeignKey 字段设置初始值时遇到问题。我有一个CreateView 表单,我正在从具有 url 的书籍详细信息视图中访问它:path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'), 我想在我输入 CreateView 时已经在我的表单中拥有这本书的初始值,例如 /book/4

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”

这是我的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',
    )

【问题讨论】:

    标签: python django


    【解决方案1】:
    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}
    

    它应该是那个类的对象。

    【讨论】:

    • 我很困惑如何让它工作,我需要访问 id 究竟是如何获得 pk 以重定向回正确的图书详细信息页面。不应该是这样的吗:def get_initial(self): initial = {'book': self.object.book.id} return initial
    【解决方案2】:

    让我们仔细看看BookInstance.book.id

    BookInstance -> 一个模型类

    book -> 该模型中未填充的字段(ForwardManyToOneDescriptor,重点是Descriptor

    id -> 当我们使用 Book.objects.first().id 之类的东西时,django 从数据库内部填充的字段

    因此,您尝试从模型 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})
    

    【讨论】:

    • def get_initial(self): # You could even get the Book model using Book.objects.get here! getid = Book.objects.get(Book.id) return { 'book': getid } 它引发错误:TypeError at /catalog/book/create/instance cannot unpack non-iterable DeferredAttribute object
    • 不确定你从哪里得到Book.objects.get(Book.id)。尝试在那里使用Book.objects.get(pk=self.kwargs["pk"])
    • 我得到 KeyError: 'pk' then
    • 哎呀。对不起 - 我假设您的 urlpattern 中有一个 BookInstanceCreate 视图的“pk”url 参数。你想从哪里得到 pk/id?如果您只想将其设置为静态值,则可以使用Book.objects.get(id=&lt;id for book you want to default to&gt;)
    • 我想从path('book/&lt;int:pk&gt;', views.BookDetailView.as_view(), name='book-detail'),获取一个pk参数这是一个前一页,它必须识别它是哪本书并且最初设置在一个表单中,我已经设法正确重定向到详细页面但我不知道如何从 Book 对象中获取初始 id
    猜你喜欢
    • 2010-10-23
    • 2014-04-18
    • 1970-01-01
    • 2018-12-17
    • 2010-12-02
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多