Octavian Niculescu
Octavian Niculescu

Reputation: 445

Having trouble linking two fields with one another, then implementing them to another model

I'm working on a project which helps users find jobs.

So one of the models, named Oferta is used for details about a job. Someone who is looking for emplooyes, just completes a form which is based on this model, and people will be looking at it.

Here's this model:

class Oferta(models.Model):
    solicitant = models.ForeignKey(User, on_delete=models.CASCADE)
    cor = models.CharField(max_length=50)
    dataSolicitare = models.DateField(default=date.today)
    denumireMeserie = models.CharField(max_length=50)
    locuri = models.IntegerField()
    agentEconomic = models.CharField(max_length=50)
    adresa = models.CharField(max_length=150)
    dataExpirare = models.DateField()
    experientaSolicitata = models.CharField(max_length=200)
    studiiSolicitate = models.CharField(max_length=200)
    judet = models.CharField(max_length=20)
    localitate = models.CharField(max_length=25)
    telefon = models.CharField(max_length=12)
    emailContact = models.EmailField(max_length=40)
    rezolvata = models.BooleanField(default=False)
    def __str__(self):
        return self.cor

The COR field is the code asociated to a job. Also, denumireMeserie means job name.

So these should be linked. Let's say, if code 1 means "Cook", these should be link - there will be no other job with a different code, or another job for code 1.

So, in my opinion, these two fields should have a OneToOne relationship between them, if I'm not mistaken.

But these codes and jobs need to be implemented in the database - so they need a model too.

class CORMeserii(models.Model):
    CodCOR = models.CharField(max_length=25, primary_key=True, unique=True)
    MeserieCor = models.OneToOneField(CodCOR, max_length=50, unique=True, on_delete=models.CASCADE)

And here is how I tried to do it, but obviously it won't work, because onetoonefield needs a model as the first parameter.

So, my questions are:

How can I link these two fields as I told you, and then link Oferta.cor to CORMeserii.CodCOR and Oferta.denumireMeserie to CORMeserii.MeserieCor?

(because each job with its code and name should be implemented in the database, then chosen in each Oferta (which means offer))

Upvotes: 0

Views: 30

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

As Dirk pointed out on your previous question, you have not understood what relationship fields do in Django.

A ForeignKey or a OneToOneField gives you access to the entire related object. This means you can access any of the fields on that related object.

So your Oferta model does not need a denumireMeserie field; that belongs on the other model, which we might call "Job". Oferta has a link to that model, ie a ForeignKey:

class Oferta(models.Model):
    solicitant = models.ForeignKey(User, on_delete=models.CASCADE)
    job = models.ForeignKey('Job', on_delete=models.CASCADE)

and Job has the details of the job:

class Job(models.Model):
    cor = models.CharField(max_length=50)
    denumireMeserie = models.CharField(max_length=50)

Now you can create a Job and an Oferta for that job:

my_job = Job.objects.create(cor=1, denumireMeserie='Cook')
my_oferta = Job.objects.create(job=my_job, ...rest of the fields...)

Now you can access the job name via the relationship:

print(my_oferta.job.denumireMeserie)

which will give you "Cook".

Upvotes: 1

Related Questions