Home > OS >  Failed to save in SQLite database django
Failed to save in SQLite database django

Time:12-01

I have the following codes:

models.py

class Job(models.Model):

    jobname = models.CharField(max_length = 1000)
    owner = models.CharField(max_length = 150)
    enabled = models.BooleanField()
    freq_type = models.IntegerField(default = 1)
    freq_interval = models.IntegerField(default = 0)
    freq_recurrence = models.IntegerField(default = 0)
    start_date=models.CharField(max_length=10)
    end_date=models.CharField(max_length=10, blank = True)
    start_time=models.CharField(max_length=6)
    end_time=models.CharField(max_length=6, blank = True)
    date_added = models.DateTimeField(auto_now_add = True, null = True)
    date_modified=models.DateTimeField(auto_now = True, null = True)
    version=models.IntegerField(default = 1)

class Job_removed(models.Model):

    jobname = models.CharField(max_length = 1000)
    owner = models.CharField(max_length = 150)
    enabled = models.BooleanField(null = True)
    freq_type = models.IntegerField(default = 1)
    freq_interval = models.IntegerField(default = 0)
    freq_recurrence = models.IntegerField(default = 0)
    start_date=models.CharField(max_length=10)
    end_date=models.CharField(max_length=10, blank = True)
    start_time=models.CharField(max_length=6)
    end_time=models.CharField(max_length=6, blank = True)
    date_added = models.DateTimeField(null = True)
    date_modified=models.DateTimeField(default=timezone.now)
    version=models.IntegerField(null=True)

views.py

def job_delete(request,pk):
    job=Job.objects.get(pk=pk)
    jobdetail = Job_detail.objects.get(job=pk)
    
    if request.method == "POST":
        jobr = JobRemovedForm(request.POST)
        
        if jobr.is_valid():
            jobr.jobname = job.jobname
            print(jobr.jobname)
            jobr.owner = job.owner
            print(jobr.owner)
            jobr.enabled = job.enabled
            print(jobr.enabled)
            jobr.start_date = job.start_date
            print(jobr.start_date)
            jobr.start_time = job.start_time
            print(jobr.start_time)
            jobr.date_added = job.date_added
            print(jobr.date_added)
            jobr.version = job.version
            print(jobr.version)
            jobr.save()
            return redirect('/job/', {'job':Job.objects.all})
        else:
            jobr = JobRemovedForm()
           
            return render(request, 'interface/job_removed.html', {'job':job, 'jobdetail':jobdetail, 'jobr':jobr})
    return render(request, 'interface/job_removed.html', {'job':job, 'jobdetail':jobdetail})

Output of my powershell for those print commands:

enter image description here

In the database (SQLite):

enter image description here

What I am trying to do is to copy from the entry from Job Table to Job_removed Table. I assign the new entry in Job_removed with the values in Job Table. It is printing correctly in my powershell but when I check my database, none of the value are entering. Why is this happening though? Can anyone explain to me and point me to the right direction to correct this? I know there are post about how to clone data to another table but it does not fit the task that I am required to do so I am not using those answers.

Update: model for Job_detail and form for JobRemovedForm

models.py

class Job_detail(models.Model):
    job_type=models.IntegerField(default=1)
    json = models.CharField(max_length = 1000)
    job = models.ForeignKey(Job, on_delete=models.CASCADE)

forms.py

class JobRemovedForm(ModelForm):
    class Meta:
        model = Job_removed
        fields = []

Update 2: views (I realize i didnt do commit=False) and form fields updated

views.py

def job_delete(request,pk):
    job=Job.objects.get(pk=pk)
    jobdetail = Job_detail.objects.get(job=pk)
    
    if request.method == "POST":
        jobr = JobRemovedForm(request.POST)
        
        if jobr.is_valid():
            jr = jobr.save(commit=False)
            jr.jobname = job.jobname
            print(jr.jobname)
            jr.owner = job.owner
            print(jr.owner)
            jr.enabled = job.enabled
            print(jr.enabled)
            jr.start_date = job.start_date
            print(jr.start_date)
            jr.start_time = job.start_time
            print(jr.start_time)
            jr.date_added = job.date_added
            print(jr.date_added)
            jr.version = job.version
            print(jr.version)
            jr.save()
            return redirect('/job/', {'job':Job.objects.all})
        else:
            print(jobr.errors)
            jobr = JobRemovedForm()
            
            return render(request, 'interface/job_removed.html', {'job':job, 'jobdetail':jobdetail, 'jobr':jobr})
    return render(request, 'interface/job_removed.html', {'job':job, 'jobdetail':jobdetail})

forms.py

class JobRemovedForm(ModelForm):
    class Meta:
        model = Job_removed
        fields = ['jobname', 'owner', 'enabled', 'start_date', 'start_time', 'date_added', 'version']

And now my powershell is showing jobr.errors of the following:

  • jobname
  • This field is required.
  • owner
  • This field is required.
  • start_date
  • This field is required.
  • start_time
  • This field is required.
  • date_added
  • This field is required.
  • version
  • This field is required.
  • CodePudding user response:

    Try removing fields attribute from JobRemovedForm or you could mention all the fields you want to save.

    Recommended approach is to use class-based in-built Django CreateView so that with a few lines of code you could achieve the same.

    CodePudding user response:

    Actually you do not need Job_removed model because it is unuseful and not better design for a such case in general.so first remove that model and add a field called is_deleted to your job model which value should be True for deleted jobs and False for non deleted jobs.by default i make is_deleted is False so when you deleted it you can mark it as True

    class Job(models.Model):
    
        jobname = models.CharField(max_length = 1000)
        owner = models.CharField(max_length = 150)
        enabled = models.BooleanField()
        freq_type = models.IntegerField(default = 1)
        freq_interval = models.IntegerField(default = 0)
        freq_recurrence = models.IntegerField(default = 0)
        start_date=models.CharField(max_length=10)
        end_date=models.CharField(max_length=10, blank = True)
        start_time=models.CharField(max_length=6)
        end_time=models.CharField(max_length=6, blank = True)
        date_added = models.DateTimeField(auto_now_add = True, null = True)
        date_modified=models.DateTimeField(auto_now = True, null = True)
        version=models.IntegerField(default = 1)
        is_delete = models.BooleanField(default=False) # new field
    
    1. Delete your model remove Job_removed 2)run python manage.py makemigrations 3)run python manage.py migrate

    now let us work on your views for deleting jobs.

    from django.shortcuts import render,get_object_or_404,redirect
    def job_delete(request,pk):
        job= get_object_or_404(Job,pk=pk,is_deleted=False)
        job.is_deleted = True # delete the job if it is not deleted 
        job.save()
        return redirect('/job/')
    

    Note:I use get_object_or_404 to raise page not found if there is no job related to the pk and i check that the job is not deleted.

    now i do not know how is your other views but you should now make a little bit of change in querying jobs.if you want to query all jobs you should query jobs that is not deleted.by doing this

    Job.objects.filter(is_deleted = False)
    

    instead of

    Job.objects.all()
    

    and better approach should be to use post method for deleting jobs not get.but for now you can keep as it is.
    sorry for my english if you do not understand please ask me in the comments.

    • Related