【问题标题】:Django check box from models WITHOUT using the form.py?没有使用 form.py 的模型中的 Django 复选框?
【发布时间】:2021-01-13 07:06:51
【问题描述】:

如何在 Django 中仅使用 model.py 文件而不使用 form.py 文件来呈现允许多选的复选框?

这是我的代码(但仅适用于下拉单选而不是复选框多选):

from django.db import models

class PropertyAmenities(models.Model):

# here below is the list of multiple choices that I would like the user to be able to select
PROPERTY_AMENITIES_CHOICES = (
    ('maid_room', 'MAID_ROOM'),
    ('study','STUDY'),
    ('balcony_or_terrace', 'BALCONY_OR_TERRACE'))

# the below only work as "dropdown single choice". How can I have it as a "multiple choice check box"?
property_amenities = models.CharField(max_length=20, choices=PROPERTY_AMENITIES_CHOICES)

问题:如何在不接触或使用form.py文件的情况下修改模型类?

【问题讨论】:

    标签: django django-models django-rest-framework django-views django-forms


    【解决方案1】:

    找到的最佳解决方案是以下库:“django-multiselectfield” 执行以下步骤并将以下内容用作基本指南:

    pip install django-multiselectfield
    

    或者,如果您使用 pycharm 或任何其他 IDE,请从解释器设置/库部分安装库:“django-multiselectfield”

    转到 settings.py 文件并在 INSTALLED_APPS 中添加以下内容:

    'multiselectfield'
    

    进入model.py文件:

    from django.db import models
    from multiselectfield import MultiSelectField
    
    MY_CHOICES = (('maid', 'MAID'),
                  ('study', 'STUDY'),
                  ('balcony', 'BALCONY'))
    
    
    class MyModel(models.Model):
        my_field = MultiSelectField(choices=MY_CHOICES)
    
        def __str__(self):
            return self.my_field
    

    转到 views.py 文件: 从 django.shortcuts 导入渲染 从 .models 导入 MyModel 从 django.views 导入视图 从 django.views.generic 导入 CreateView 从 django.urls 导入 reverse_lazy

    class Index(View):
        def get(self, request):
            return render(request, 'multi/index.html', {'my_model_all': MyModel.objects.all()})
    
    
    class CreateMyModel(CreateView):
       model = MyModel
       fields = '__all__'
       success_url = reverse_lazy('multi:home')
    

    转到 urls.py 文件(显然是您的应用程序):

    from django.urls import path
    from .views import *
    
    app_name = 'multi'
    urlpatterns = [
        path('', Index.as_view(), name='home'),
        path('jack/create/', CreateMyModel.as_view(), name='create_rock')
    ]
    

    最后确保在我的情况下,模板/your_app_folder 中有“templates/multi”以下文件:“index.html”、“mymodel_form.html”。

    index.html 文件如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Index</title>
    </head>
    <body>
    <a href="{% url 'multi:create_rock' %}">Add Form</a>
    </body>
    </html>
    

    mymodel_form.html 如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Multi Checkbox Form</title>
    </head>
    <body>
    <form method="post">
        {% csrf_token %}
        {{form}}
        <input type="submit">
    </form>
    </body>
    </html>
    

    在运行服务器之前,不要忘记在保存所有文件后执行 manage.py makemigrations 和迁移。 运行一个manage.py 检查以获得良好的度量。 最后运行服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      相关资源
      最近更新 更多