'Difference between import appname.models and from appname.models.ModelName in Django

As a programming style, I find it more useful to write the following:

import appname.models 
obj = appname.models.ModelName.objects.filter(status=1)

However, I find lot of code written where only the ModelName is imported:

from appname.models import ModelName
obj = ModelName.objects.filter(status=1)

However, I often come across the scenario, where the change in the ModelName result in the application breaking and we need to make changes everywhere.

Also, from a readability perspective, the second approach, I find it very difficult know, where was the model defined(which app).

I have been insisting my team to go with the first approach. Wanted to hear your thoughts which is the best approach from the following perspective:

  1. Readability
  2. Scalability
  3. Performance

I really appreciate all your thoughts here.



Solution 1:[1]

from appname.models import ModelName

I prefer this one becuase this way tell us more clearly about model and model comes from which apps. When u are working of big project using api or Thiry party apps and lots of importing models or other party apps so it difficult to find models or some other stuff. In short way i prefer this one

Solution 2:[2]

import appname.models

This method is not preferred. Because it adds nothing to code base. You have to useappname.models again and again. Code quality will be compromised and loading time will also increase in bigger apps.

from appname.models import ModelName

While this approach is more appropriate and efficient as well. You will be importing the Specified model only once and will be using it again and again. This approach is efficient in memory and in loading time as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanveer Ahmad
Solution 2 maadi