django 后台添加一个管理药品信息的功能模型
时间: 2024-03-01 19:49:59 浏览: 29
好的,下面是在 Django 后台添加一个管理药品信息的功能模型的步骤:
1. 在应用的 models.py 文件中创建药品信息模型类,定义模型的字段,例如:
```python
from django.db import models
class Drug(models.Model):
name = models.CharField(max_length=50, verbose_name='药品名称')
price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='价格')
manufacturer = models.CharField(max_length=50, verbose_name='生产厂家')
production_date = models.DateField(verbose_name='生产日期')
shelf_life = models.IntegerField(verbose_name='保质期(月)')
description = models.TextField(verbose_name='描述')
class Meta:
verbose_name_plural = '药品信息' # 指定后台管理中显示的名称
ordering = ['-id'] # 指定按 id 倒序排列
def __str__(self):
return self.name
```
2. 在应用的 admin.py 文件中注册药品信息模型类,并自定义展示字段,例如:
```python
from django.contrib import admin
from .models import Drug
class DrugAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'manufacturer', 'production_date', 'shelf_life')
list_filter = ('manufacturer', 'production_date')
search_fields = ('name', 'manufacturer')
admin.site.register(Drug, DrugAdmin)
```
在自定义展示字段中,我们指定了药品名称、价格、生产厂家、生产日期和保质期,在药品信息列表中将会显示这些字段。同时,我们也指定了根据生产厂家、生产日期进行筛选,根据药品名称、生产厂家进行搜索。
3. 运行 Django 项目并登录后台管理页面,就可以看到已经添加的药品信息模型。
通过这样的方式,我们就可以在 Django 后台管理页面中添加一个管理药品信息的功能模型。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
{% extends "admin/base.html" %} {% block content %} 药品列表 ID 药品名称 操作 {% for drug in drugs %} {{ drug.id }} {{ drug.drug_name }} 编辑 <form action="{% url 'admin:drug_db_drug_list_delete' drug.id %}" method="post" class="d-inline"> {% csrf_token %} <button class="btn btn-danger btn-sm">删除</button> </form> {% empty %} 暂无药品 {% endfor %} 添加新药品 药品相互作用 ID 药品1 药品2 相互作用 操作 {% for interaction in interactions %} {{ interaction.id }} {{ interaction.drug1 }} {{ interaction.drug2 }} {{ interaction.interaction }} 编辑 <form action="{% url 'admin:drug_db_drug_interaction_delete' interaction.id %}" method="post" class="d-inline"> {% csrf_token %} <button class="btn btn-danger btn-sm">删除</button> </form> {% empty %} 暂无药品相互作用 {% endfor %} 添加新药品相互作用 {% endblock %} 需要定义那些视图和建那些文件
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)