如何将没有html表单的数据保存到Django的mysql数据库中

时间:2019-07-12 10:07:00

标签: mysql django python-3.x database

我想在Django中将数据添加到mysql数据库中,而无需在post方法中使用任何形式的表单。我想对数据进行硬编码并将其存储在数据库中。我不想从phpmyadmin存储它并想对其进行编码,而我已经将我的models.py文件制成了。这样就在数据库中创建了表“ offesnses”。

models.py

from django.db import models
from django.core.validators import int_list_validator
# Create your models here.

class offenses(models.Model):
    oid = models.IntegerField(primary_key=True)
    description = models.CharField(null=False, max_length=20)
    assigned_to = models.CharField(null=True, max_length=20)
    categories = models.TextField(null=True)
    category_count = models.IntegerField(null=True)
    policy_category_count = models.IntegerField(null=True)
    security_category_count = models.IntegerField(null=True)
    close_time = models.TimeField(null=True)
    closing_user = models.IntegerField(null=True)
    closing_reason_id = models.IntegerField(null=True)
    credibility = models.IntegerField(null=True)
    relevance = models.IntegerField(null=True)
    severity = models.IntegerField(null=True)
    magnitude = models.IntegerField(null=True)
    destination_networks = models.TextField(null=True)
    source_network = models.CharField(null=True, max_length=20)
    device_count = models.IntegerField(null=True)
    event_count = models.IntegerField(null=True)
    flow_count = models.IntegerField(null=True)
    inactive = models.BooleanField(null=True)
    last_updated_time = models.DateTimeField(null=True)
    local_destination_count = models.IntegerField(null=True)
    offense_source = models.IntegerField(null=True)
    offense_type = models.IntegerField(null=True)
    protected = models.BooleanField(null=True)
    follow_up = models.BooleanField(null=True)
    remote_destination_count = models.IntegerField(null=True)
    source_count = models.IntegerField(null=True)
    start_time = models.TimeField(null=True)
    status = models.CharField(null=True, max_length=20)
    username_count = models.IntegerField(null=True)
    source_address_ids = models.TextField(null=True)
    local_destination_address_ids = models.TextField(null=True)
    domain_id = models.IntegerField(null=True)
    class Meta:
        db_table = "offenses"

我想将response_body数据保存到数据库中,例如ID,说明等

views.py

response_body = json.loads(response.read().decode('utf-8'))

for j in response_body:
    print(j['id'])


context = {
    'ids': [j['id'] for j in response_body],
    'response': response_body,
    'dict_values': {'a': 1, 'b': 2}
}

return render(request, 'table.html', context)

我上网冲浪,我所能找到的只是通过不是我所需要的表格插入数据。

django和python的新功能非常感谢。

0 个答案:

没有答案