don´t create duplicated objects. django, python

Viewed 62

I created a script to avoid creating duplicate objects but it still created the same objects when I run the command 3 times it creates them 3 times over and over again. I would like you to help me and know what is wrong with my code.

from django.core.management.base import BaseCommand
from jobs.models import Job
import json
from datetime import datetime
import dateparser


class Command(BaseCommand):
    help = 'Set up the database'

    def handle(self, *args: str, **options: str):
        with open('static/newdata.json', 'r') as handle:
            big_json = json.loads(handle.read())
            for item in big_json:
                if len(item['description']) == 0:
                    print('Not created. Description empty')
                    continue

                dt = dateparser.parse(item['publication_date'])

                existing_job = Job.objects.filter(

                    job_title = item['job_title'],
                    company = item['company'],
                    company_url = item['company_url'],
                    description = item['description'],
                    publication_date = dt,
                    salary = item['salary'],
                    city = item['city'],
                    district = item['district'],
                    job_url = item['job_url'],
                    job_type = item['job_type'],

                )
                if existing_job.exists() is True:
                    print('This Job already exist')
                else:
                    Job.objects.create(

                        job_title = item['job_title'],
                        company = item['company'],
                        company_url = item['company_url'],
                        description = item['description'],
                        publication_date = dt,
                        salary = item['salary'],
                        city = item['city'],
                        district = item['district'],
                        job_url = item['job_url'],
                        job_type = item['job_type'],

                )

                self.stdout.write(self.style.SUCCESS('added jobs!'))
4 Answers

try

if existing_job.exists():

instead of

if existing_job.exists() is True:

because .exists() returns boolean itself

Have you tried using unique_together without the publication_date field? Docs

# models.py
class Job(models.Model):
    # Your fields here...

    class Meta:
        unique_together = [[
            'job_title',
            'company',
            'company_url',
            'description',
            'salary',
            'city',
            'district',
            'job_url',
            'job_type'
        ]]

dt = dateparser.parse(item['publication_date'])
new_date = date(dt.year, dt.month, dt.day)

here was the problem. I am scraping the publication date in this format ('1 Week Ago') and then I was changing to a date and time format. and when I run the script again the time of the conversion is a different time. so that's why the job is created again. because is not the same because the time creation

Related