Re: [Tutor] model methods in Django

2013-05-19 Thread eryksun
On Sun, May 19, 2013 at 10:49 AM, Matthew Ngaha  wrote:
> On Sun, May 19, 2013 at 3:34 PM, Steven D'Aprano  wrote:
>> Matthew, who are you quoting? Your email program should automatically insert
>> an attribution line, such as the one just below. Without that attribution
>> line, it is hard to follow the conversation, as we can't tell who you are
>> quoting.
>
> ok thanks i didnt think about that. I was quoting eryksun. I'll
> includethat line from now on

I think this was in reference to your reply to John Steedman. Here's
the archive thread (based on the "In-Reply-To" header field):

http://mail.python.org/pipermail/tutor/2013-May/thread.html#95521
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Matthew Ngaha
On Sun, May 19, 2013 at 3:34 PM, Steven D'Aprano  wrote:
> Matthew, who are you quoting? Your email program should automatically insert
> an attribution line, such as the one just below. Without that attribution
> line, it is hard to follow the conversation, as we can't tell who you are
> quoting.
>
> On 20/05/13 00:23, Matthew Ngaha wrote:

ok thanks i didnt think about that. I was quoting eryksun. I'll
includethat line from now on
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Steven D'Aprano

Matthew, who are you quoting? Your email program should automatically insert an 
attribution line, such as the one just below. Without that attribution line, it 
is hard to follow the conversation, as we can't tell who you are quoting.

On 20/05/13 00:23, Matthew Ngaha wrote:

class Poll(models.Model):

[...]


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Matthew Ngaha
 > options.py is the biggest module in the admin package. The link I
> posted is to the get_actions method of ModelAdmin. In the tutorial,
> PollAdmin extends this class.
>
oh ok thanks, yes i will definately look through it

> I'm not coming from any framework. My knowledge of web development is
> scattered from bits learned here and there, out of curiosity rather
> than necessity. With Python 3 you're a bit limited, but you still have
> options. Obviously there's Django -- arguably the most popular,
> batteries-included framework. Pyramid is a leaner alternative. Bottle
> and CherryPy (also a WSGI server) are micro frameworks. There's also
> Tornado if you want an asynchronous framework based on callbacks and
> coroutines.

As there are a lot more guides for Django i think i will stick with it
rather than Pyramid. Maybe after ive learnt it well i can pick up
Cherrypy as i like the sounds of a small framework with a server. But
Django will be my main focus. Thanks for detailing the options for
Python 3
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Matthew Ngaha
> class Poll(models.Model):
>
> question = models.CharField(max_length=200)
> pub_date = models.DateTimeField('date published')
>
>
>
> 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?'
>
>
> Source:
> https://docs.djangoproject.com/en/dev/intro/tutorial01/
> https://docs.djangoproject.com/en/dev/intro/tutorial02/
>
>
> I find the Django (and this fragment) very elegant UP TO the comment.
>
is the dislike after the comment more to do with the python code
rather than django itself?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread eryksun
On Sun, May 19, 2013 at 7:20 AM, Matthew Ngaha  wrote:
> Thanks that does clear it up. Also thats a huge script you linked, can

options.py is the biggest module in the admin package. The link I
posted is to the get_actions method of ModelAdmin. In the tutorial,
PollAdmin extends this class.

> i ask which framework you're coming from, and why you have decided to
> try out django? When i asked around, people's opinions of it weren't
> good and i was recommended to try flask but as i only use Python 3, i
> won't be able to use Flask.

I'm not coming from any framework. My knowledge of web development is
scattered from bits learned here and there, out of curiosity rather
than necessity. With Python 3 you're a bit limited, but you still have
options. Obviously there's Django -- arguably the most popular,
batteries-included framework. Pyramid is a leaner alternative. Bottle
and CherryPy (also a WSGI server) are micro frameworks. There's also
Tornado if you want an asynchronous framework based on callbacks and
coroutines.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread John Steedman
For the benefit of others,I believe the full class (from the Django
Tutorial) is

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

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?'


Source:
https://docs.djangoproject.com/en/dev/intro/tutorial01/
https://docs.djangoproject.com/en/dev/intro/tutorial02/


I find the Django (and this fragment) very elegant UP TO the comment.


On Sat, May 18, 2013 at 8: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 has me
> really confused.
>
> The model definition is omitted but can anyone explain how this methed
> (was_published_recently) is given these attributes:
>
> 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.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Matthew Ngaha
> The default description is the name with underscores removed, unless
> you set a custom description in the function's "short_description"
> attribute. I'm not experienced with Django, so I can't ramble off lots
> of examples, but hopefully you get the gist.

Thanks that does clear it up. Also thats a huge script you linked, can
i ask which framework you're coming from, and why you have decided to
try out django? When i asked around, people's opinions of it weren't
good and i was recommended to try flask but as i only use Python 3, i
won't be able to use Flask.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread eryksun
On Sun, May 19, 2013 at 6:35 AM, Matthew Ngaha  wrote:
>
> if you look at the diagram under that function, why is the value of
> "was_published_recently.short_description" the title of that field?
> replacing the old title? is a "short_desccription" attribute set
> somewhere in django being inactive, but once you make use of this
> attribute(or keyword), whatever the value is automatically becomes the
> field title? is it the same for boolean being the value of the field
> with the checked or unchecked symbol?

For example, see line 586 in admin/options.py:

https://github.com/django/django/blob/1.5.1/django/contrib/admin/options.py#L586

# Gather actions from the admin site first
for (name, func) in self.admin_site.actions:
description = getattr(func, 'short_description',
name.replace('_', ' '))
actions.append((func, name, description))

The default description is the name with underscores removed, unless
you set a custom description in the function's "short_description"
attribute. I'm not experienced with Django, so I can't ramble off lots
of examples, but hopefully you get the gist.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-19 Thread Matthew Ngaha
Thanks guys i had no idea about these  method attributes and also
these underlying oop  __objects__

@ eryksun
i understand your explanation, im still having trouble figuring out
how django is being used in the tutorial.

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?'
list_filter = ['pub_date']
search_fields = ['question']
date_hierarchy = 'pub_date'


if you look at the diagram under that function, why is the value of
"was_published_recently.short_description" the title of that field?
replacing the old title? is a "short_desccription" attribute set
somewhere in django being inactive, but once you make use of this
attribute(or keyword), whatever the value is automatically becomes the
field title? is it the same for boolean being the value of the field
with the checked or unchecked symbol?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-18 Thread eryksun
On Sat, May 18, 2013 at 10:22 PM, Dave Angel  wrote:
> 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.

Django uses function attributes as metadata. The names "boolean" and
"short_description" are self-documenting. "admin_order_field" is
explained here:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/
#django.contrib.admin.ModelAdmin.list_display

Usually, elements of list_display that aren’t actual database
fields can’t be used in sorting (because Django does all the
sorting at the database level). However, if an element of
list_display represents a certain database field, you can
indicate this fact by setting the admin_order_field attribute
of the item.

The Poll model is part of the tutorial, "Writing your first Django app":

https://docs.djangoproject.com/en/1.5/intro

The function attributes are added in "Customize the admin change
list", in part 2.

> 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.

In a class definition, from a conceptual point of view, you're adding
a 'method'. But technically it's a function object. When accessed as
an attribute, the function's __get__ descriptor is used to create a
method on the fly.

The instancemethod type has a custom __getattribute__ that first
checks the method object's attributes such as __self__. If the lookup
on the method object fails, it proxies the __getattribute__ of the
wrapped __func__. For example:

class Spam(object):
def __repr__(self):
return 'eggs'

>>> meth = Spam().__repr__
>>> type(meth)

>>> meth.__self__
eggs

A method doesn't have a __dict__ for setting dynamic attributes:

>>> meth.boolean = False
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'instancemethod' object has no attribute 'boolean'

But you can set attributes on the underlying function object:

>>> type(meth.__func__)

>>> meth.__func__.boolean = False

The method will proxy them:

>>> meth.boolean
False

But not for assignment:

>>> meth.boolean  = True
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'instancemethod' object has no attribute 'boolean'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] model methods in Django

2013-05-18 Thread Dave Angel

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] model methods in Django

2013-05-18 Thread Matthew Ngaha
 im following the official docs and after learning Python im sure of
how methods work, but the model example on the beginners guide has me
really confused.

The model definition is omitted but can anyone explain how this methed
(was_published_recently) is given these attributes:

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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor