Values from class to class

28 views
Skip to first unread message

Carlos Andre

unread,
Jul 6, 2015, 8:29:35 PM7/6/15
to django...@googlegroups.com
Hello to all , I need to solve a problem. I have two classes of which have to use , in the second class, the first coming values. How to do this?
Thanks for listening!

James Schneider

unread,
Jul 7, 2015, 3:08:58 AM7/7/15
to django...@googlegroups.com

Can you give a simple example or analogy of what you are trying to do?

-James

On Jul 6, 2015 5:29 PM, "Carlos Andre" <euca...@gmail.com> wrote:
Hello to all , I need to solve a problem. I have two classes of which have to use , in the second class, the first coming values. How to do this?
Thanks for listening!

--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA8yBMx1B0VAyA4qqCM4SMhYdUAo9Hv_p4yvdMD1d_3R8PbdeA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Carlos Andre

unread,
Jul 7, 2015, 2:00:16 PM7/7/15
to django...@googlegroups.com
example : I have two classes, respectively person contact.
I want to contact class has , in its attributes , values ​​worked coming from class person , such as name, pass in person , and size of the contact name in the class.
I did something like this :
from django.db import models

class person ( models.Model ) :
    qtdade = models.IntegerField (default = 1 )

    class Meta :
        verbose_name_plural = ' Person '

    def valor_contato ( self):
        return self.qtdade * 2

class contact ( models.Model ) :
    p = Person ()
    value = models.CharField ( max_length = 150, default = p.qtdade )

James Schneider

unread,
Jul 7, 2015, 2:43:40 PM7/7/15
to django...@googlegroups.com
On Tue, Jul 7, 2015 at 10:59 AM, Carlos Andre <euca...@gmail.com> wrote:
> example : I have two classes, respectively person contact.
> I want to contact class has , in its attributes , values worked coming from
> class person , such as name, pass in person , and size of the contact name
> in the class.

Ahh, what you probably want is a foreign key relationship from Contact
back to Person. See below.

I'm not sure what you mean by size, though. Is that a reference to the
qtdade field?

> I did something like this :
> from django.db import models
>
> class person ( models.Model ) :
> qtdade = models.IntegerField (default = 1 )
>
> class Meta :
> verbose_name_plural = ' Person '
>
> def valor_contato ( self):
> return self.qtdade * 2
>
> class contact ( models.Model ) :
> p = Person ()
> value = models.CharField ( max_length = 150, default = p.qtdade )
>

What you probably want is this:

class Contact( models.Model ) :
p = models.ForeignKey(Person, null=False)


Note that I used an uppercase letter to start your class (Contact).
Python classes in general use the CapWords naming convention. See PEP8
(https://www.python.org/dev/peps/pep-0008/#class-names).

This makes the Person attributes available via the Contact model
class, so you can do something like this:

a_person = Person.objects.create(qtdade=3)

a_contact = Contact.objects.create(p=a_person)

print(a_contact.p.qtdade)

##########
# Would print out 3
##########

Any attributes that are added to the Person class would then be
available to a Contact using the syntax I set above. See the
documentation on relationships between models:

https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-one-relationships

As far as this line goes:

> value = models.CharField ( max_length = 150, default = p.qtdade )

It looks like you are trying to set a default in the model definition
that is dependent on a field in another model. Django doesn't support
this because this is a single value that is applied directly in the
database on the column during a migration/database initialization
(therefore, no objects exist in order to determine what that value
should be on a fresh migration). In general, you would set such a
value inside of the save() method for your model whenever a Contact is
saved, and use default='' (or default=None, but not recommended for
CharField fields) in the model definition above.

See this link for more information regarding the save() method on models:

https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods

Carlos Andre

unread,
Jul 7, 2015, 6:34:52 PM7/7/15
to django...@googlegroups.com
i trying use thats solutions, in real yet continuos the problem.
The value, when are to use in other class, return to defaut value without get the value who i send in the field.


--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Carlos Andre

unread,
Jul 7, 2015, 7:31:52 PM7/7/15
to django...@googlegroups.com
I want copy some values to other fields in other table!

James Schneider

unread,
Jul 7, 2015, 9:06:28 PM7/7/15
to django...@googlegroups.com
On Tue, Jul 7, 2015 at 4:31 PM, Carlos Andre <euca...@gmail.com> wrote:
> I want copy some values to other fields in other table!
>

That's fine, although I would only recommend doing so if the qtdade in
Person and 'value' field in Contact can contain different data in some
scenario. You will still need to set a static default (ie default='')
in the model definition and override the save() method to do the copy,
though:

# This is a bit naive and assumes that p always contains a Person
object. Add validation and/or business logic accordingly.

def save(self, *args, **kwargs):
# If no PK, we know this is a new object and that value hasn't been set
if not self.pk:
self.value = self.p.qtdade
super(Contact, self).save(*args, **kwargs) # Call the "real" save() method.


If they will always be the same, I would recommend keeping the
approach that I sent out originally, and in addition, adding a
property or model method to retrieve that data in the Contact class.

-James
Reply all
Reply to author
Forward
0 new messages