0

I have the following model:

class Check (models.Model):
    
    user_id     = models.ForeignKey(Client, on_delete=models.CASCADE)
    client = Client.objects.get(pk=1) #change id according to user_id
    client_birth = client.birth
    client_gender = client.gender

What I need is to get those birth and gender values from model Client, but according to the value id set on user_id. Is that possible?

  • You mean check model will have data of only client with pk 1? Inshort you are declaring one model with one data? looks wrong to me .. – Neeraj Mar 31 '21 at 17:35
  • @Neeraj No. With `pk=1` is working just for one entry from Client model.What I need is to change that `pk=1` for something so that client values changes according to the foreign key set on `user_id`. – Juan Camilo Orjuela Mar 31 '21 at 17:50
  • This part does not go in models: client = Client.objects.get(pk=1) #change id according to user_id client_birth = client.birth client_gender = client.gender where have you written this ? in viewset? – Neeraj Mar 31 '21 at 17:54
  • I think that you are trying to use the database approach when creating the Foreign Key and establishing the relationship between the models. In Django world when setting relationships you have to rely on Django ORM. Setting Many To One relationship: https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_one/ . One To One relationship: https://docs.djangoproject.com/en/3.1/topics/db/examples/one_to_one/ – alv2017 Mar 31 '21 at 19:32
  • @Neeraj. No. It is in the Model, as I need those data for some calculated fields in the Check model. – Juan Camilo Orjuela Mar 31 '21 at 23:58

3 Answers3

0

Currently you have check.user_id as a Client object, and then check.client as another Client object matching client_id 1.

I'm not sure from your wording but I think that your expectation is that user_id is pulling an id (integer) value and the client is fetching the Client object with that user ID.

Can you explain a bit more about what you're trying to do?

Edit

Based on your comment I'm still not entirely sure, but I think what you want is this. Lets pretend what you want the Check class to do is just print out some basic info about the client to the screen in the command line. Obviously your do_stuff method would be something else.

from django.db import models

class Client(models.Model):
    name = models.CharField(limit=50)
    birth = models.DateField()
    gender = models.Charfield()
    
class Check (models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)

    def do_stuff(self):
        print(f'This is the client name: {self.client.name}')
        print(f'This is the client birth date: {self.client.birth_date}')
        print(f'This is the client gender: {self.client.gender}')

You don't need to store the birth and gender as their own Check attributes if you're going to be accessing them from within the model. Its cleaner just to reference them through the client object.

Then to invoke your class and method you would do the following:

client = Client.objects.create(name='Dave', birth='2000-01-01', gender='male')
checker = Check.objects.create(client=client)
checker.do_stuff()

If you want to store birth and gender as client_birth = client.birth or client_gender = client.gender to pass to a view or something else, you can do that as well.

David Grenier
  • 1,221
  • 1
  • 10
  • 23
  • I have a Client model with some data. I need that data in the Check model so I can calculate some fields for a physical check. That is why I'm using the ForeignKey field, but there might be a more useful way to solve this. – Juan Camilo Orjuela Apr 01 '21 at 00:00
0

If I got your question right, the answer should be here:

Accessing Model Data in another model/form

Cheers

mike_thecode
  • 171
  • 2
  • 6
0

Models are the skeleton of your database tables, all the business operations go in forms/viewsets/serializer.

What you are trying to do here is :

class Check (models.Model):
    user_id = models.ForeignKey(Client, on_delete=models.CASCADE)
    client = Client.objects.get(pk=1) #change id according to user_id
    client_birth = client.birth
    client_gender = client.gender
    
    
class Client (models.Model):
    gender = models.CharField()
    birth = models.DateField()

The result would be:

## Client Table ##

id      gender      birth
1       M           1/1/89
2       F           2/2/90
3       M           3/3/91


## Check Table ##

id      user_id     
1       1

It wont create anything else in check table.

Your code models should be like this :

class Check (models.Model):
    user_id = models.ForeignKey(Client, on_delete=models.CASCADE)
    # (Assuming there are other fields in Check model , else theres no use of this)
    
class Client (models.Model):
    gender = models.CharField()
    birth = models.DateField()

Then in your custom create/update method you have to write the logic on what you have to check:

user_id = (you get it from request or user input maybe , not sure what your requirements are)
client = Client.objects.get(pk=user_id)
client_birth = client.birth
client_gender = client.gender
# Rest of the code and save/update

In my opinion Check model wont make any sense, it has to go in forms/serializer where you are actually checking before creating or saving it in your DB.

Neeraj
  • 975
  • 4
  • 10
  • I don't agree with this assessment necessarily. Many django programmers put most of the logic as methods in the model, and then use views simply to trigger specific methods. It really depends on your overall programming approach and the needs of your project. – David Grenier Apr 01 '21 at 14:37