[Tutor] model methods in Django
Dave Angel
davea at davea.name
Sun May 19 04:22:15 CEST 2013
On 05/18/2013 03:16 PM, Matthew Ngaha wrote:
> im following the official docs and after learning Python im sure of
> how methods work, but the model example on the beginners guide
which official docs? URLs please?
which beginners guide? URL please?
> has me
> really confused.
>
I don't know Django, so if this is really Django specific, I can't help.
> The model definition is omitted but can anyone explain how this methed
> (was_published_recently) is given these attributes:
Which attributes are you confused about?
The admin_order_field, boolean, ande short_description attributes of the
method are bound in class code, immediately after the method is defined.
The pub_date is probably an instance attribute of either the Poll class
or the models.Model class. It should probably be defined in the
appropriate __init__ method. In any case it's not a method attribute.
>
> class Poll(models.Model):
> # ...
> def was_published_recently(self):
> return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
> was_published_recently.admin_order_field = 'pub_date'
> was_published_recently.boolean = True
> was_published_recently.short_description = 'Published recently?'
>
> are the names of the attributes already attached to these
> functions/methods, or are they being created on the fly with whatever
> name you want? As i am unable to comprehend what is going on, i dont
> really have a clue as to what each definition is doing and how it
> affects the model, even after reading this section of the docs over
> and over again im still lost.
>
This fragment isn't big enough for much else to be told. But I don't
really understand what aspect is confusing you.
Perhaps you didn't realize that a function can have attributes, and that
they can be added to the function at any time after the function is
created. Being a method doesn't change that.
--
DaveA
More information about the Tutor
mailing list