On Tue, Jul 7, 2015 at 10:59 AM, Carlos Andre <eucan...@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

>
> 2015-07-07 4:08 GMT-03:00 James Schneider <jrschneide...@gmail.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" <eucan...@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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciVWhKYeG5jV%2B00epKyOQv2zSJWVq%2BFg-XstRrP%2Bp0mdBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to