Django Separating models from models.py

I am trying to separate all models from a file models.py. What I'm doing is mentioned in this link . But the problem in my model is this django.contrib.auth.user, and I am pushing one function in models.py as follows to generate a token.

def create_user_profile(sender, instance, created, **kwargs):  
    if created:  
        UserProfile.objects.create(user=instance)  

post_save.connect(create_user_profile, sender=User)

So, how do I import this thing into a file _init_.pyas we import the model as

from myapp.models.foo import Foo
+4
source share
3 answers

You should only have models.pyor models/__init__.py, and it looks like you have both. One of these modules is likely to be shadowed by the other, so they do not have both (i.e. get rid of models.py)

+1
source

, User __init__.py. , create_user_profile. :

from django.contrib.auth.models import User
0

You cannot import a command, but, for example, by importing the above function, it provides the start of the connect call. A function call in the body of the models.py file is also performed from the same raeson (i.e. Models are imported).

# p.py: 
print "hello"   # a command executed while importing anything
def x():        # a definition that can be imported
    pass

# python shell
>>> from p import x
hello
>>> 
0
source

All Articles