Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 25, 2011 11:22:32

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


Hi,

can I use methods of methods in Django?

Like so:

class Car(models.Model):
producer = models.ForeignKey(CarProducer)
country_of_origin = producer.country_of_origin

This doesn't seem to work. Is there a special syntax for this?

Jaroslav

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 25, 2011 11:30:57

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


On 25 jan, 11:57, Jaroslav Dobrek <jaroslav.dob...@gmail.com> wrote:
> Hi,
>
> can I use methods of methods in Django?

What's a "methods of methods" ???

> Like so:
>
> class Car(models.Model):
>     producer = models.ForeignKey(CarProducer)
>     country_of_origin = producer.country_of_origin

What is CarProducer.country_of_origin ? A method ? A model.Field ?
Something else ?

> This doesn't seem to work.

What happens ? And what did you expect to happen ?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 25, 2011 11:37:01

Euan G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


I agree with the previous poster - the title is misleading as the word
"method" is incorrect in both places.

It seems that the original poster is talking about denormalizing data.
However, this is unnecessary as the ORM allows for this type of data
to be retrieved any how, e.g.
Car.objects.all().select_related(depth=1) will allow statements like:
car.producer.country_of_origin

If you want access to country_of_origin directly on the Car instances,
I'd suggest a property:

@property
def country_of_origin(self):
return self.producer.country_of_origin

Euan

On Jan 25, 11:30 am, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 25 jan, 11:57, Jaroslav Dobrek <jaroslav.dob...@gmail.com> wrote:
>
> > Hi,
>
> > can I use methods of methods in Django?
>
> What's a "methods of methods" ???
>
> > Like so:
>
> > class Car(models.Model):
> >     producer = models.ForeignKey(CarProducer)
> >     country_of_origin = producer.country_of_origin
>
> What is CarProducer.country_of_origin ? A method ? A model.Field ?
> Something else ?
>
> > This doesn't seem to work.
>
> What happens ? And what did you expect to happen ?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 25, 2011 13:04:06

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


>> What happens ?

I get this error message when I run manage.py syncdb:

AttributeError: 'ForeignKey' object has no attribute 'producer'

>> And what did you expect to happen ?

I want the 'country_of_origin' of cars to be the string that is stored
in their producer's 'country_of_origin':

class CarProducer(models.Model):
country_of_origin = models.CharField(max_length=200)


class Car(models.Model):
    producer = models.ForeignKey(CarProducer)
    country_of_origin = producer.country_of_origin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 25, 2011 13:04:28

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


On 25 Jan., 12:36, Euan Goddard <euan.godd...@gmail.com> wrote:
> I agree with the previous poster - the title is misleading as the word
> "method" is incorrect in both places.

O.k. sorry. Should have been "attribute of attribute", right?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 25, 2011 13:05:25

n.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


Hi there,

Are you sure that you want to denormalize your schema like that ?
I now this is a totally different issue and question but in this case
do you really need to perform this operation ? Have considered the
problems that this approach will bring?

Imagine this simple use case:
If one changes the country_of _orign of the Producer that wont reflect
on the Car instance!
I can see some utility for this use but in any case I would review my
requirements.

Cheers,
N.

On Jan 25, 12:36 pm, Euan Goddard <euan.godd...@gmail.com> wrote:
> I agree with the previous poster - the title is misleading as the word
> "method" is incorrect in both places.
>
> It seems that the original poster is talking about denormalizing data.
> However, this is unnecessary as the ORM allows for this type of data
> to be retrieved any how, e.g.
> Car.objects.all().select_related(depth=1) will allow statements like:
> car.producer.country_of_origin
>
> If you want access to country_of_origin directly on the Car instances,
> I'd suggest a property:
>
> @property
> def country_of_origin(self):
>    return self.producer.country_of_origin
>
> Euan
>
> On Jan 25, 11:30 am, bruno desthuilliers
>
>
>
>
>
>
>
> <bruno.desthuilli...@gmail.com> wrote:
> > On 25 jan, 11:57, Jaroslav Dobrek <jaroslav.dob...@gmail.com> wrote:
>
> > > Hi,
>
> > > can I use methods of methods in Django?
>
> > What's a "methods of methods" ???
>
> > > Like so:
>
> > > class Car(models.Model):
> > >     producer = models.ForeignKey(CarProducer)
> > >     country_of_origin = producer.country_of_origin
>
> > What is CarProducer.country_of_origin ? A method ? A model.Field ?
> > Something else ?
>
> > > This doesn't seem to work.
>
> > What happens ? And what did you expect to happen ?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 25, 2011 13:17:54

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


On 25 jan, 12:41, Jaroslav Dobrek <jaroslav.dob...@gmail.com> wrote:
> >> What happens ?
>
> I get this error message when I run manage.py syncdb:
>
> AttributeError: 'ForeignKey' object has no attribute 'producer'
>
> >> And what did you expect to happen ?
>
> I want the 'country_of_origin' of cars to be the string that is stored
> in their producer's 'country_of_origin':

Do you mean that you want Car to have a country_of_origin model.Field
(IOW: the yourapp_car table to have a country_of_origin column) and
copy the related Producer.country_of_origin field's value in it, or
just Car instances to have a "country_of_origin" attribute that is
just a shortcut for self.producer.country_of_origin ?


> class CarProducer(models.Model):
>   country_of_origin = models.CharField(max_length=200)
>
> class Car(models.Model):
>     producer = models.ForeignKey(CarProducer)
>     country_of_origin = producer.country_of_origin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Jan. 25, 2011 14:19:52

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


On Tue, Jan 25, 2011 at 11:41 AM, Jaroslav Dobrek
<jaroslav.dob...@gmail.com> wrote:
>>> What happens ?
>
> I get this error message when I run manage.py syncdb:
>
> AttributeError: 'ForeignKey' object has no attribute 'producer'
>
>>> And what did you expect to happen ?
>
> I want the 'country_of_origin' of cars to be the string that is stored
> in their producer's 'country_of_origin':
>
> class CarProducer(models.Model):
>  country_of_origin = models.CharField(max_length=200)
>
>
> class Car(models.Model):
>     producer = models.ForeignKey(CarProducer)
>     country_of_origin = producer.country_of_origin
>

class Car(models.Model):
producer = models.ForeignKey(CarProducer)

@property
def country_of_origin(self):
return self.producer.country_of_origin


Cheers

Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 Jan. 26, 2011 11:56:07

Jaroslav D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

method of method


Thanks to all of you.

Now I understand this better.

Jaroslav

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support