how can I reference a field from one model to another model

32 views
Skip to first unread message

carlos

unread,
Jan 24, 2019, 1:29:34 AM1/24/19
to django...@googlegroups.com
Hello, 
I do not know if I'm asking the question correctly, but I need to call a field of one model in another model
example:
class ModelDad(models.Model):
    name = blablabla

class ModelChild1(....):
   fk(ModelDad)
   number = models.IntegerField()

class ModelChild2(....):
  fk(ModelDad)
  another_number = models.IntegerField()

   def make_proces(self):
      return self.another_number * ModelChild1.number #this is my question

how can I send a call number within the function (make_proces ) of ModelChild2

is posible????



--
att.
Carlos Rocha

Mike Dewhirst

unread,
Jan 25, 2019, 1:50:26 AM1/25/19
to django...@googlegroups.com
You need a mechanism to find ModelChild1 and the Django ORM provides
Queries for exactly that.

There is a default model manager called 'objects' which you can use to
find instances of models. By that I mean you can pinpoint a row in the
database table which that model represents. For example ... (but to make
it clearer I'll adjust your models above slightly)

class ModelDad(models.Model):
    name = blablabla

class ModelChild1(models.Model):
   dad = models.ForeignKey(ModelDad)
   number = models.IntegerField()

class ModelChild2(models.Model):
   dad = models.ForeignKey(ModelDad)
   another_number = models.IntegerField()

   def make_proces(self):
        child1 = ModelChild1.objects.get(dad=self.dad)
        return self.another_number *child1.number

Visit Django documentation https://docs.djangoproject.com and scroll
down past "First steps" to  "The model layer" and click on a link in the
"Quersets" line. The answers will be in there.

>
> is posible????
>
>
>
> --
> att.
> Carlos Rocha
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAM-7rO125r3pox%3D137GOfmjnmFaf3cekaNj-j%2B9A0Cwc%3DEM0Fg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAM-7rO125r3pox%3D137GOfmjnmFaf3cekaNj-j%2B9A0Cwc%3DEM0Fg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

NAveeN Kumar Reddy

unread,
Jan 25, 2019, 6:59:07 AM1/25/19
to django...@googlegroups.com
You can use this where you want you to link tables with foreign key

models.ForeignKey(Model_Name,on_delete=models.CASCADE)

That's it  both tables will be in relation after migration of models into database



To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Naveen Kumar  Reddy

Anirudh Jain

unread,
Jan 25, 2019, 7:14:24 AM1/25/19
to django...@googlegroups.com
You can do this easily by creating creating an object of that model from which you want to get the value or input a value by using 'get_object_or_404' or 'get_list_or_404' by importing from django.shortucts. Reas about these two functions and difference between them in documentation.

Use them, for eg :-
 derive = get_object_or_404(ModelChild1, ModelDad=request.user) #object created of model ModelChild1

access = derive.number #will give the value of number that has been input by user

Amitesh Sahay

unread,
Jan 25, 2019, 7:49:39 AM1/25/19
to django...@googlegroups.com
hi, 

All the above should work. If you are working on django REST, then you can use ModelSerializer helper function. This will automatically sync with the table fields, and if there are any foreign key and primary key. They would be mapped automatically. 

Regards,
Amitesh Sahay
91-750 797 8619


carlos

unread,
Jan 25, 2019, 6:13:32 PM1/25/19
to django...@googlegroups.com
Thank Mike Dewhirst is working well

Cheers


For more options, visit https://groups.google.com/d/optout.


--
att.
Carlos Rocha
Reply all
Reply to author
Forward
0 new messages