Select2 小部件显示在 Django 管理站点上,但不显示在 Django4 中的表单模板上

发布于 2025-01-11 12:09:27 字数 2220 浏览 0 评论 0原文

我有两个对象模型:NewsObject 和 StockObject。股票对象是新闻对象中的外键。

class stockObject(models.Model):
    stock_name = CharField(max_length=100, blank=True, null=True)
    stock_tag = CharField(max_length=100, blank=True, null=True)

    def __str__(self):
        return self.stock_name

class newsObject(models.Model):
    title = CharField(max_length=100, blank=True, null=True)
    body = TextField(blank=True, null=True)
    stock = ForeignKey(stockObject, on_delete=models.SET_NULL, blank=True, null=True)

我在 ModelAdmin 类中使用了 autocomplete_fields 属性,因为我想要新闻中股票的可搜索下拉列表。我还在股票 ModelAdmin 中添加了 search_fields,如 文档

这就是我的 admin.py 的样子:

class stockAdmin(admin.ModelAdmin):
    list_display = ['stock_name', 'stock_tag']
    search_fields = ['stock_name']

class newsAdmin(admin.ModelAdmin):
    list_display = ['title', 'body', 'stock']
    search_fields = ['title', 'body', 'stock']
    autocomplete_fields = ['stock']

现在,问题是我在 Django 管理站点上获得了该字段的可搜索下拉列表,但它只是实际模板屏幕上的一个下拉列表(不可搜索)。我有一个调用模板的基本视图,如下所示:

Views.py

def createNews(request):
    form = NewsForm()
    if request.method == 'POST':
        form = NewsForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/backoffice/')

    context = {'form' : form}
    return render(request, 'NewsForm.html', context)

和 NewsForm.html 是:

{% extends "base.html" %}

{% load static %}
       {% block content %} 
       <form action="" method="POST">
           {% csrf_token %}
           {{ form }}
           <input type="submit" name="Submit">
       </form>
{% endblock %} 

我一直想知道这种行为的原因可能是什么。尝试了多种方法,但都不起作用。我在这里可能会缺少什么?

Django 管理站点图片Django 管理站点图片

Django 模板图片Django 模板图像

I have two object models, NewsObject and StockObject. The stock object is a foreign key in the news object.

class stockObject(models.Model):
    stock_name = CharField(max_length=100, blank=True, null=True)
    stock_tag = CharField(max_length=100, blank=True, null=True)

    def __str__(self):
        return self.stock_name

class newsObject(models.Model):
    title = CharField(max_length=100, blank=True, null=True)
    body = TextField(blank=True, null=True)
    stock = ForeignKey(stockObject, on_delete=models.SET_NULL, blank=True, null=True)

I have used autocomplete_fields property in the ModelAdmin class as I want a searchable dropdown for stocks in news. I have also added search_fields in the stocks ModelAdmin as mentioned in the documentation.

This is what my admin.py looks like:

class stockAdmin(admin.ModelAdmin):
    list_display = ['stock_name', 'stock_tag']
    search_fields = ['stock_name']

class newsAdmin(admin.ModelAdmin):
    list_display = ['title', 'body', 'stock']
    search_fields = ['title', 'body', 'stock']
    autocomplete_fields = ['stock']

Now, the issue is that I get a searchable dropdown on the Django Admin site for this field, but it is only a dropdown (not searchable) on the actual template screen. I have a basic view which calls the template, like so:

Views.py

def createNews(request):
    form = NewsForm()
    if request.method == 'POST':
        form = NewsForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/backoffice/')

    context = {'form' : form}
    return render(request, 'NewsForm.html', context)

And NewsForm.html is:

{% extends "base.html" %}

{% load static %}
       {% block content %} 
       <form action="" method="POST">
           {% csrf_token %}
           {{ form }}
           <input type="submit" name="Submit">
       </form>
{% endblock %} 

I've been wondering what might be the cause of this behavior. Tried multiple things but none of them work. What might I be missing here?

Django Admin site imageDjango Admin site image

Django Template ImageDjango Template Image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2025-01-18 12:09:27

我认为您已经用 camelCase 编写了所有模型,因此首先将它们更改为 PascalCase

其次,您在所有模型中都错过了models

像这样编写它们,在每个数据类型之前添加models,例如:

from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

不仅是字段的datatype

I think you have written all your models in camelCase so first changed them to PascalCase.

Second, you have missed models in all your models:

Write them like this add models before every datatype like:

from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

Not only datatype of fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文