0

I'm writing a web scraper to get information about customers and appointment times to visit them. I have a class called Job that stores all the details about a specific job. (Some of its attributes are custom classes too e.g Client).

class Job:

def __init__(self, id_=None, client=Client(None), appointment=Appointment(address=Address(None)), folder=None,
             notes=None, specific_reqs=None, system_notes=None):
    self.id = id_
    self.client = client
    self.appointment = appointment
    self.notes = notes
    self.folder = folder
    self.specific_reqs = specific_reqs
    self.system_notes = system_notes

def set_appointment_date(self, time, time_format):
    pass

def set_appointment_address(self, address, postcode):
    pass

def __str__(self):
    pass

My scraper works great as a stand alone app producing one instance of Job for each page of data scraped. I now want to save these instances to a Django database.

I know I need to create a model to map the Job class onto but that's where I get lost. From the Django docs (https://docs.djangoproject.com/en2.1/howto/custom-model-fields/) it says in order to use my Job class in the Django model I don't have to change it at all. That's great - just what I want. but I can't follow how to create a model that maps to my Job class.

Should it be something like

from django.db import models
import Job ,Client


class JobField(models.Field):
    description = "Job details"

    def __init__(self, *args, **kwargs):
        kwargs['id_'] = Job.id_
        kwargs['client'] = Client(name=name)
        ...
        super().__init__(*args, **kwargs)


class Job(models.Model):

    job = JobField()

And then I'd create a job using something like

Job.objects.create(id_=10101, name="Joe bloggs")

What I really want to know is am I on the right lines? Or (more likely) how wrong is this approach? I know there must be a big chunk of something missing here but I can't work out what.

0

1 Answer 1

1

By mapping I'm assuming you want to automatically generate a Django model that can be migrated in the database, and theoretically that is possible if you know what field types you have, and from that code you don't really have that information.

What you need to do is to define a Django model like exemplified in https://docs.djangoproject.com/en/2.1/topics/db/models/.

Basically you have to create in a project app's models.py the following class:

from django import models


class Job(models.Model):
    client = models.ForeignKey(to=SomeClientModel)
    appointment = models.DateTimeField()
    notes = models.CharField(max_length=250)
    folder = models.CharField(max_length=250)
    specific_reqs = models.CharField(max_length=250)
    system_notes = models.CharField(max_length=250)

I don't know what data types you actually have there, you'll have to figure that out yourself and cross-reference it to https://docs.djangoproject.com/en/2.1/ref/models/fields/#model-field-types. This was just an example for you to understand how to define it.

After you have these figured out you can do the Job.objects.create(...yourdata).

You don't need to add an id field, because Django creates one by default for all models.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. This looks like I'm writing a completely new class that has the same attributes as my existing Job class. Am I correct? If that's the case what do I do about the class methods. Do I duplicate those as well?
@SteveStrop depends on the methods. Some of them that handle instances could go in an Model Manager, others could be declared as properties, etc.
There's a problem with this code sample: you are using self outside a method. When you define fields in Django you do that at the class level. There's no need to use self.myfield (that's actually wrong and won't work).
@Eric Chiesse indeed! I did a copypaste error. corrected the code sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.