On Mon, Aug 01, 2016 at 05:09:37AM -0700, graeme wrote: > 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)?
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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/20160801122834.GB5430%40koniiiik.org. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: Digital signature