models.Model

24 views
Skip to first unread message

youpsla

unread,
Oct 16, 2011, 6:43:56 PM10/16/11
to Django users
Hello,
I'm new to Django and I've only a medium background in funtionnal
programming in Python.

I'm currently reading and working on the Django tutorial. I've few
questions on a example of code because things are not so clear for me.

Here is the code example

1 from django.db import models
2
3 class Person(models.Model):
4 first_name = models.CharField(max_length=30)
5 last_name = models.CharField(max_length=30)

Can you say me if I'm wrong or not on the followings:

- line 1 : Import of the" models" method from the "django.db"
module ?

- line 3 : Définition of the "Person" classs wich heritates from the
"models.Model class ? If true, and assuming that "models" is a method,
a class can be nested in a method ?

- ligne 4 : Creation of variable "first_name" by calling the method
CharField with one argument. But how "CharFiled" is written, isn't it
a class ? ANd here we use only "models", then why on line 3 we use
"models.Model" ?

I hope it's not to much confusing !!!! :-)

Help would be REALLY appreciated !!!

Regards

Alain

Daniel Roseman

unread,
Oct 17, 2011, 4:08:02 AM10/17/11
to django...@googlegroups.com
This is fairly basic stuff - sounds like you would benefit from doing an introductory Python tutorial.

At the root of your confusion, I think, is that you're mixing up methods and modules. `models` in the code above is a module[*] containing multiple classes. You can't import a method, as that's simply a function belonging to a class. You can import modules - `from django.db import models` - or elements such as classes and constants directly inside a module - `from django.db.models import Model`.

So, the `models` module includes a class named Model, and also several other classes which represent fields - CharField, BooleanField, etc. All of these live directly inside the models namespace, so are referred to in exactly the same way - models.Model, models.CharField, etc.
--
DR.

[*] Yes, strictly speaking it's a package, but that's probably too confusing at this stage

Webb Newbie

unread,
Oct 17, 2011, 2:53:03 AM10/17/11
to django...@googlegroups.com
"models" is a module that contains classes Model and CharField.

Person inherits from Model.

first_name and last_name are instances of CharField.

There's no functional programming here.

-WN.

youpsla

unread,
Oct 17, 2011, 7:04:41 AM10/17/11
to Django users
Thanks you 2 for your answers.

I think I've understood now. Again I apologize for the newby Python
question but I've read three times "Dive in Python" and it hasn't made
me thnigs clear. That's the reason why I've posted.

I thought there was a hierarchy like this models.Model.CharField ....

That's the reason why I didn't understood.

Daniel, you said, that a method can be imported, this mean that an
external method (method from a class like Model) can't be used
directly in the Person class for example ?

If I understand well, in "first_name =
models.CharField(max_length=30)", first_name is an instance of class
CharField. This instance send "30" to the "max_length" parameter of
class CharField. This parameter is used by an internal method of class
CharFiled. Is that right ? :-)

Thanks again to all

Regards

Alain

youpsla

unread,
Oct 17, 2011, 7:18:00 AM10/17/11
to Django users
hello again, sorry for eventually annoying. But later in the Django
tutorial,

there is this code:

from django.conf.urls import patterns, include, url
.....................................
urlpatterns = patterns('',
(r'^polls/$', 'polls.views.index'),
......................)),
)

What I understand here is that "patterns", "include" and "url" modules
are imported.

After the result of the function "patterns" is assigned to
"urlpatterns". AS Daniel said that a function can't be imported, this
means that a method can be called (but not imported) or this means
that a function and a method are not the same thing ?

Regards

Alain

On 17 oct, 08:53, Webb Newbie <new...@avatarific.com> wrote:

Daniel Roseman

unread,
Oct 17, 2011, 7:25:04 AM10/17/11
to django...@googlegroups.com
On Monday, 17 October 2011 12:04:41 UTC+1, youpsla wrote:
Thanks you 2 for your answers.

I think I've understood now. Again I apologize for the newby Python
question but I've read three times "Dive in Python" and it hasn't made
me thnigs clear. That's the reason why I've posted.

I thought there was a hierarchy like this models.Model.CharField ....

That's the reason why I didn't understood.

Daniel, you said, that a method can be imported, this mean that an
external method (method from a class like Model) can't be used
directly in the Person class for example ?

If I understand well, in "first_name =
models.CharField(max_length=30)", first_name is an instance of class
CharField. This instance send "30" to the "max_length" parameter of
class CharField. This parameter is used by an internal method of class
CharFiled. Is that right ? :-)

Thanks again to all

Regards

Alain

I'm having a little difficulty understanding your question (problème de traduction, sans aucun doute). Person inherits from models.Model, so all methods that are defined in the superclass are also available to the subclass - that's how Person gets methods such as `save`.

As for `max_length=30`, when you instantiate a class Python calls the `__init__` method of that class, which in this case accepts a number of arguments including max_length.
--
DR.

Daniel Roseman

unread,
Oct 17, 2011, 7:27:01 AM10/17/11
to django...@googlegroups.com


On Monday, 17 October 2011 12:18:00 UTC+1, youpsla wrote:
hello again, sorry for eventually annoying. But later in the Django
tutorial,

there is this code:

from django.conf.urls import patterns, include, url
.....................................
urlpatterns = patterns('',
    (r'^polls/$', 'polls.views.index'),
    ......................)),
)

What I understand here is that "patterns", "include" and "url" modules
are imported.

After the result of the function "patterns" is assigned to
"urlpatterns". AS Daniel said that a function can't be imported, this
means that a method can be called (but not imported) or this means
that a function and a method are not the same thing ?

Regards

Alain

I didn't say that a function can't be imported. It definitely can. As you say, function and method aren't the same thing - a method is a function that belongs to a class. It's not possible to import just a method, because there's no way of referring to it except via the class. But anything at the base level of a module - whether it's a class, a function, a variable, anything - *can* be imported.
--
DR. 

youpsla

unread,
Oct 17, 2011, 10:00:09 AM10/17/11
to django...@googlegroups.com
Hello Daniel
thanks for your answer and sorry for my english. As you said, it could be a problem of translation !!! :-)

I really appreciate your help, I'm doing lots of progress in my python object programming curve ...

OK, then to be sure how Python works ... let see this code:

1 from django.db import models 

3 class Person(models.Model): 
4     first_name = models.CharField(max_length=30) 
5     last_name = models.CharField(max_length=30) 

Ligne 1 imports module "models". I've browse the source code of Django. In the "models" folder, there is a __init__.py file and a lots of .py other files.
There is folders too and specially one called "fields".

Here is my question :
  • When importing models on line 1, doest it import all the .py files in this module, then  all classes, Class, functions, variables at the top level of each .py are available for use ? Or there is only an automatic import of the __init__.py ?

Line 4 : : The CharField Class definition is in models/fields/__init__.py. In the code above, there nowhere a reference at "fields". But the line 10 of the the __init__.py in models is " This file is automatically loaded by Python. In this file on line 10, there is "from django.db.models.fields import *". "

Here is my question :
  • Does it means that the CharField Class is available inside Person Class because there is a cascading import following this way:
    • models contains an __init__.py wich import fields and __init.py in fields has a definition of Class CharField on line 601
Hope it's enugh clear ... don't spend much time when it is not and just ask for reformulation, I'll dot it ...
Regards

Alain


Daniel Roseman

unread,
Oct 18, 2011, 5:08:14 AM10/18/11
to django...@googlegroups.com
On Monday, 17 October 2011 15:00:09 UTC+1, youpsla wrote:
Hello Daniel
thanks for your answer and sorry for my english. As you said, it could be a problem of translation !!! :-)

I really appreciate your help, I'm doing lots of progress in my python object programming curve ...

OK, then to be sure how Python works ... let see this code:

1 from django.db import models 

3 class Person(models.Model): 
4     first_name = models.CharField(max_length=30) 
5     last_name = models.CharField(max_length=30) 

Ligne 1 imports module "models". I've browse the source code of Django. In the "models" folder, there is a __init__.py file and a lots of .py other files.
There is folders too and specially one called "fields".

Here is my question :
  • When importing models on line 1, doest it import all the .py files in this module, then  all classes, Class, functions, variables at the top level of each .py are available for use ? Or there is only an automatic import of the __init__.py ?
Only the __init__.py. But... 

Line 4 : : The CharField Class definition is in models/fields/__init__.py. In the code above, there nowhere a reference at "fields". But the line 10 of the the __init__.py in models is " This file is automatically loaded by Python. In this file on line 10, there is "from django.db.models.fields import *". "

Here is my question :
  • Does it means that the CharField Class is available inside Person Class because there is a cascading import following this way:
    • models contains an __init__.py wich import fields and __init.py in fields has a definition of Class CharField on line 601

Yes, this is exactly right.
 
Hope it's enugh clear ... don't spend much time when it is not and just ask for reformulation, I'll dot it ...
Regards

Alain



You might like to read the effbot's explanation of the different import methods:

--
DR. 
Reply all
Reply to author
Forward
0 new messages