Better style: from django.db import models vs from django.db.models import ...

53 views
Skip to first unread message

graeme

unread,
Aug 1, 2016, 8:09:37 AM8/1/16
to Django users
I have always imported models, and then:

class Foo(models.Model):
    bar = models.CharField(max_length=100)


which is what the examples in the django docs do - I copied it when I started using Django and the habit stuck.

It is a lot less verbose to do:

from models import Model, CharField

class Foo(Model):
    bar =CharField(max_length=100)

because each imported class is used many times in a typical models file. The same apples to forms.

On the other hand the generic views documentation tends to do things like:

from django.views.generic.detail import DetailView

I have increasingly tended to do the same with forms, but not for models, except for things like Q which would be verbose to do otherwise

Which is better style (and why)?

ludovic coues

unread,
Aug 1, 2016, 8:17:15 AM8/1/16
to django...@googlegroups.com
A models file might use a lot of class from the django.db.models
module and importing each class lead to lengthy import line. That's
might be one reason.
The better reason might be that a lot of these class are named the
same in django.forms and django.db.models. When you type
models.CharField, you know you are working with a models field.

There is a balance to find between explicit and implicit, complexity,
readability and purity at the price of practicality.

I'm curious of what other people thinks of that.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f644ac2f-098a-4bdb-a67e-d17e762a4ce7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Michal Petrucha

unread,
Aug 1, 2016, 8:29:17 AM8/1/16
to django...@googlegroups.com
It is mostly a matter of taste. When you just do
``from django.db import models``, you can use whatever is defined
inside models by just accessing it through the models module; if you
import things one by one, you have to list all of them explicitly in
the import statement. Either way is fine, using models.IntegerField
and forms.IntegerField just makes it more obvious what kind of
IntegerField it is you're referring to.

The only thing I can think of is that you might sometimes want to
define your own model field, in which you override the default
formfield; in that case, you'd need to have both model fields and form
fields in the same module, and it would be quite chaotic and unclear
which one you're referring to if you cherry-picked classes to import
instead of just importing the modules.

Cheers,

Michal
signature.asc

graeme

unread,
Aug 3, 2016, 10:52:56 AM8/3/16
to Django users


On Monday, August 1, 2016 at 5:59:17 PM UTC+5:30, Michal Petrucha wrote:
On Mon, Aug 01, 2016 at 05:09:37AM -0700, graeme wrote:
Either way is fine, using models.IntegerField
and forms.IntegerField just makes it more obvious what kind of
IntegerField it is you're referring to.
 
I always have forms and models defined in separate files so it is a non-issue. If it is in models.py it is models.IntegerField etc.

The only thing I can think of is that you might sometimes want to
define your own model field, in which you override the default
formfield; in that case, you'd need to have both model fields and form
fields in the same module, and it would be quite chaotic and unclear
which one you're referring to if you cherry-picked classes to import
instead of just importing the modules.

Good point. In that particular file I would import models and forms for clarity. Probably put that in a separate file and import just my custom model field into models.py

Cheers,

Michal
Reply all
Reply to author
Forward
0 new messages