0

In Django rest framework, I have different models inside model folder -

settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'rest_framework',
'myapp',
]


AUTH_USER_MODEL = 'myapp.models.UserModel.User'
  • myapp
    • settings
      • dev.py
    • models
      • UserModels.py
      • OrderModels.py
      • ReportingModels.py

UserModels.py

class User(AbstractBaseUser, GuardianUserMixin ,PermissionsMixin):

id = models.AutoField(_('id'),unique=True,primary_key=True)
email = models.EmailField(_('email address'),unique=True)
last_name = models.CharField(_('last name'), max_length=100, blank=True)
first_name = models.CharField(_('first name'), max_length=100, blank=True)
parent_id = models.IntegerField(_('parent id'), default=0)
organization_id = models.IntegerField(_('organization id'), default=0, null=True)
created_at = models.DateTimeField(_('date joined '), default=timezone.now())
updated_at = models.DateTimeField(_('date modified '), default=timezone.now())
incorrect_login = models.IntegerField(_('incorrect login frequency'), default=0)
soft_delete = models.BooleanField(_('soft delete'), default=False, )
group = models.ForeignKey('auth.Group', null=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name, first_name, email,organization_id,group_id ']

class Meta:
    app_label = 'zeocuser'
    db_table = 'zeocuser_zeocuser'
    permissions = (
        (
            ('add_user_admin', 'add user admin'),
        )
    )


objects = UserManager()

Now, in settings.py for AUTH_USER_MODEL if i give value as following - AUTH_USER_MODEL = 'myapp.models.UserModel.User' it doesn't accept it and give following error -

File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/contrib/admin/models.py", line 32, in <module>
    class LogEntry(models.Model):
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/base.py", line 158, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 706, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 306, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 86, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/fields/related.py", line 84, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "/Users/richagupta/VirtualEnvs/py35/lib/python3.5/site-packages/django/db/models/utils.py", line 13, in make_model_tuple
    app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

But If I put my user model directly inside the app in a models.py file (without putting it in models folder) and give

AUTH_USER_MODEL = 'myapp.User'

It works fine. But in that case, how should I organise my other model classes. Kindly suggest .

1 Answer 1

0

Django expects the models to be located in the .models namespace.

As it's mentioned in point 2 at https://docs.djangoproject.com/en/1.9/ref/applications/#how-applications-are-loaded:

You must define or import all models in your application’s models.py or models/init.py. Otherwise, the application registry may not be fully populated at this point, which could cause the ORM to malfunction.

In your case, You'll need to import the models in myapp/models/__init__.py file:

from .UserModels import User, <other models>
from .OrderModels import <other models>
from .ReportingModels import <other models>

and so on

Sign up to request clarification or add additional context in comments.

4 Comments

Sure, I am facing another problem in django. To make an api with fliter capability , something like /user?email=<> , in that, In the urls.py i have url(r'^api/v1/users/$', UserViews.UserList.as_view(), name='userlist_view'), url(r'^api/v1/users/(?P<email>.+)/$', UserViews.UserList.as_view(), name='userList_view'), But /user?email=<> always go to the method corresponding to api/v1/users/ . In views, I have added method- def get_queryset(self): email = self.kwargs.get(self.lookup_url_kwarg) return User.objects.filter(email=email) But control doesn't go there .
you should try with /users/?email=<> (note the / before the ?)
No It doesn't work.Even on trying : localhost:8000/api/v1/users/[email protected]/ it goes to get all users method
For more detials, please see : stackoverflow.com/questions/36569058/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.