简体   繁体   English

如何在 python Django 中保存到数据库之前进行逻辑操作

[英]How to do a logical operation before saving to database in python Django

So i am new to Django, and i want to make some hardware compatibility check feature in my app, i make a form to be filled and check the compatibility from database then show me the result.所以我是 Django 的新手,我想在我的应用程序中做一些硬件兼容性检查功能,我制作一个表格来填写并检查数据库的兼容性,然后显示结果。 but the problem is the form seems to just keep saving to database without processing the logical operation first.但问题是表单似乎只是继续保存到数据库而不首先处理逻辑操作。 this is my view这是我的看法

class CreateSimView(LoginRequiredMixin, CreateView):
    login_url = '/login/'
    model = Simulation
    form_class = SimPostForm
    template_name = 'sim_post.html'
    redirect_field_name = 'sim/sim.html'


    def simpost(request):
        mtb = Motherboard.objects.all()
        cpu = Cpu.objects.all()
        vga = Vga.objects.all()
        ram = Ram.objects.all()
        storage = Storage.objects.all()

        mtbform = request.GET.get('mtb_name')
        cpuform = request.GET.get('cpu_name')
        vgaform = request.GET.get('vga_name')
        ramform = request.GET.get('ram_name')
        strform = request.GET.get('str_name')

        simcpu = cpu.objects.values_list('socket', flat=True).filter(name__icontains=cpuform)
        simcpu1 = mtb.objects.values_list('socket', flat=True).filter(name__icontains=mtbform)

        simvga = vga.objects.values_list('vga_interface', flat=True).filter(name__icontains=vgaform)
        simvga1 = mtb.objects.values_list('vga_interface', flat=True).filter(name__icontains=mtbform)

        simram = ram.objects.values_list('mem_type', flat=True).filter(name__icontains=ramform)
        simram1 = mtb.objects.values_list('mem_type', flat=True).filter(name__icontains=mtbform)

        simstr = str.objects.values_list('str_interface', flat=True).filter(name__icontains=strform)
        simstr1 = mtb.objects.values_list('str_interface', flat=True).filter(name__icontains=mtbform)

        if simcpu == simcpu1 :
            if simvga == simvga1:
                if simram == simram1:
                    if simstr == simstr1:
                        form = SimPostForm(request.POST)
                        if form.is_valid():
                            form.save()
                        return render(mtbform,cpuform,vgaform,ramform,strform,"/")
                    else:
                        strform = "not compatible"
                        return render(mtbform,cpuform,vgaform,ramform,strform,"/")
                else:
                    ramform = "not compatible"
                    return render(mtbform,cpuform,vgaform,ramform,strform,"/")
            else:
                vgaform = "not compatible"
                return render(mtbform,cpuform,vgaform,ramform,strform,"/")
        else:
            cpuform = "not compatible"
            return render(mtbform,cpuform,vgaform,ramform,strform,"/")

my models for the form我的表格模型

class Simulation(models.Model):
    build_name = models.CharField(blank=False, max_length=150)
    mtb_name = models.CharField(blank=False, max_length=150)
    cpu_name = models.CharField(blank=False, max_length=150)
    vga_name = models.CharField(blank=False, max_length=150)
    ram_name = models.CharField(blank=False, max_length=150)
    str_name = models.CharField(blank=False, max_length=150)

    def __str__(self):
        return self.build_name

    def get_absolute_url(self):
        return reverse('sim:sim_post')

and my template和我的模板

{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
  <div class="container">
    <div class="jumbotron">
      <h1>Pilih Parts</h1>
        <form method="post" enctype="multipart/form-data">
          {% csrf_token %}
          <div class="form-group">
            <label for="build">Nama Rakitan</label>
          <input class="form-control" type="text" id="build" name="build_name" value="{{ form.build_name.value|default_if_none:"" }}">
          </div>
          <div class="form-group">
            <label for="mtb">Motherboard</label>
          <input class="form-control" id="mtb" type="text" name="mtb_name" value="{{ form.mtb_name.value|default_if_none:"" }}">
        </div>
        <div class="form-group">
          <label for="vga">Vga</label>
          <input class="form-control" id="vga" type="text" name="vga_name" value="{{ form.vga_name.value|default_if_none:"" }}">
        </div>
        <div class="form-group">
          <label for="ram">Ram</label>
          <input class="form-control" type="text" id="ram" name="ram_name" value="{{ form.ram_name.value|default_if_none:"" }}">
        </div>
        <div class="form-group">
          <label for="storage">Storage</label>
          <input class="form-control" type="text" id="storage" name="str_name" value="{{ form.storage_name.value|default_if_none:"" }}">
        </div>
        <div class="form-group">
          <label for="cpu">Cpu</label>
          <input class="form-control" type="text" id="cpu" name="cpu_name" value="{{ form.cpu_name.value|default_if_none:"" }}">
        </div>
          <button type="submit" class="btn btn-secondary">Cek Kompatibilitas</button>
        </form>
    </div>
  </div>
{% endblock %}

i also use {{ form.storage_name.value|default_if_none:"" }} in my form hoping it would return the value from before.我还在我的表单中使用{{ form.storage_name.value|default_if_none:"" }}希望它会返回之前的值。 but it doesn't seem to do that.但它似乎没有这样做。

It seems to me that you implemented your logic in在我看来,你实现了你的逻辑

def simpost(request)

but you missed to integrate the call of this function in the predefined processing of the CreateView.但是您错过了将这个 function 的调用集成到 CreateView 的预定义处理中。

So one approach might be to override所以一种方法可能是覆盖

def post(self, request, *args, **kwargs)

of the CreateView and put your logic there. CreateView 并将您的逻辑放在那里。 If everything is fine, you should call如果一切正常,你应该打电话

super().form_valid(your_form_object_here)

to trigger the creation of your data with the predefined actions of the CreateView.使用 CreateView 的预定义操作触发数据的创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM