如何在Django管理界面中显示InLine对象 [英] How to display InLine objects in Django Admin Interface

查看:667
本文介绍了如何在Django管理界面中显示InLine对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的管理界面:

This is my current Admin Interface:

用户通过&模型称为 UserText。我使用NLP编写了一个仅从UserText中提取问题的函数。我希望每个单独的问题都显示在管理界面的每个用户问题部分中。截至目前,我无法使它正常工作。

A user inputs text through a form & model called "UserText". I have written a function using NLP to extract only the questions from the UserText. I would like each of these individual questions to be displayed in each "User question" section of the Admin Interface. As of now, I cannot get that to work.

这是我当前的代码:

Models.py

Models.py

class UserText(models.Model):
    user_input = models.TextField()

class Question(models.Model):
    user_text = models.ForeignKey(
        UserText,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )
    user_questions = models.CharField(max_length=2000)

Views.py

def user_text_view(request):
    form = forms.UserTextForm()
    if request.method == 'POST':
        form = forms.UserTextForm(request.POST)
        if form.is_valid():
            UserText = models.UserText
            Question = models.Question
            user_input = request.POST.get('user_input', '')
            user_input_obj = UserText(user_input = user_input)
            user_questions_obj = Question(user_text = user_input_obj,
                user_questions = Question_Init(user_input_obj))
            user_input_obj.save()
            user_questions_obj.save()
            print("Thanks for the questions!")

    else:
        form = forms.UserTextForm()

    return render(request, 'text_input_form.html', {'form': form})

Admin.py

class QuestionInLine(admin.StackedInline):
    model = Question
    display = ('user_questions_obj')


@admin.register(UserText)
class UserTextAdmin(admin.ModelAdmin):
    model = UserText
    display = ('user_input')
    inlines = [
        QuestionInLine,

    ]

最后是我的功能:

def Question_Init(user_input_obj):

    Beginning_Question_Prompts = ("Who","Whom","What","Where","When","Why","Which",
    "Whose","How","Was","Were","Did","Do","Does","Is")
    Ending_Question_Prompts = ("?",":","...")
    questions = []

    text1 = user_input_obj.user_input

    textList = sent_tokenize(text1)

    for sentence in textList:
        if sentence.startswith(Beginning_Question_Prompts):
           questions.append(sentence)

        if sentence.endswith(Ending_Question_Prompts):
            questions.append(sentence)

    return questions

我知道很多,抱歉,但是我不知道如何获取每个问题来填充管理界面中的问题字段。谢谢

I know this is a lot, sorry, but I do not know how to get each question to populate the question fields in my Admin Interface. Thanks

推荐答案

问题不是您的管理界面,而是您如何创建Question对象。您需要遍历函数的结果并为每个函数创建链接项:

The problem is not your admin interface, but how you create the Question objects. You need to iterate through the result of your function and create linked items for each one:

for question_text in Question_Init(user_input_obj):
     user_questions_obj = Question(user_text=user_input_obj,
                                   user_questions=question_text)
     user_questions_obj.save()

这篇关于如何在Django管理界面中显示InLine对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆