Thursday, March 3, 2011

Re: curious about model classes

On 3 mar, 15:16, kost BebiX <kos...@gmail.com> wrote:
> Yes, that's more a Python problem, not specifically django.
>
> You would normally do:
>
> class User(models.Model):
>     def __init__(self):
>         name = ...
>
> but this looks not cool) That's why most of python libraries use "declarative" syntax to describe models:
>
> class User(models.Model):
>     name = ...


It has nothing to do with "looking cool" or anything like that. Using
models.fields as class attributes in models class statement's body
allow for the ORM to know what db fields and relations your table has
- not what instance attributes a model instance will have. Well, not
directly at least - of course the Model base case makes sure your
instance will have corresponding instance attributes (for db fields),
but note that these instance attributes are just plain python
attributes, NOT the models.fields you defined at the class level.


> And then behind the scenes they just hide that name variable somewhere and create another name object-variable.

That's a very dumbed-down explanation of what happens.

> I think that is frustrating for newbies and lots of other users since it's hard to understand difference between object-properties and class-properties)

s/object/instance/. Python classes are objects too. And FWIW, s/
properties/attributes/, in Python 'property' is a builtin type used
for simple computed attributes.

Also and FWIW, Django is a Python framework, so "users" are supposed
to be Python programmers.

> That's one of things why python is broken, imho.

Saying so just show your lack of understanding of Python's very
powerful object model. It might not fit your brain, but that doesn't
make it "broken" in any way.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment