On 24/01/2019 5:28 pm, carlos wrote:
> 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
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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@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.
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/522c40aa-2736-3f57-b23b-9c283c9a59e0%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment