Friday, March 4, 2011

Re: curious about model classes

On Thursday, March 3, 2011 5:33:56 PM UTC+2, bruno desthuilliers wrote:
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.


Maybe you misunderstand what I was talking about. Doing

class A():
    b = SomeFieldType()

will require the "powerful python object model" (oh, I like that :-) to put in A's instance instance-variable b after all that. You say:

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

Yes. But more natural way would be to do something like
class A():
    schema = {
        'b': {'type': 'SomeType'}
    }

And that would describe class's A attribute schema that would be class-instance attribute from beginning till end (if there's no more magic), but that looks ugly and broken.

Where am I wrong?

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