I am using Django 2.2 and have two applications authentication and multi_users
There is UserSerializer in authentication application. The multi_users model has a field shared_user and thus using UserSerializer in the multi_users.serializers to serialize the shared_user field.
Now, there is AccessLevelPermissionSerializer class in the multi_users.serializers which I need in the authentication.serializers to serializer permissions of the user.
This is giving error as
ImportError: cannot import name 'AccessLevelPermissionSerializer' from 'multi_users.serializers' (/app/multi_users/serializers.py)
authentication.serializers
from authentication.models import Profile, User, Preference
class UserBaseSerializer(UserDetailsSerializer):
class Meta(UserDetailsSerializer.Meta):
fields = ('email', 'first_name', 'last_name')
class UserSerializer(UserBaseSerializer):
from multi_users.serializers import AccessLevelPermissionSerializer
permissions = AccessLevelPermissionSerializer(required=False)
class Meta(UserDetailsSerializer.Meta):
fields = UserBaseSerializer.Meta.fields + (
'permissions'
)
multi_users.serializers
from authentication.serializers import UserBaseSerializer
class SharedPermissionSerializer(serializers.ModelSerializer):
class Meta:
model = SharedPermission
fields = [
'code',
'name',
]
class AccessLevelPermissionBaseSerializer(serializers.ModelSerializer):
permission = SharedPermissionSerializer(required=False)
class Meta:
model = AccessLevelPermission
fields = [
'value',
'permission'
]
# Import this in the `authentication.serializers`
class AccessLevelPermissionSerializer(AccessLevelPermissionBaseSerializer):
pass
class MultiUserSerializer(serializers.ModelSerializer):
shared_user = UserBaseSerializer(required=False) # Use from `authentication.serializers`
class Meta:
model = MultiUser
depth = 1
fields = [
'id',
'shared_user',
'access_level',
]
How to resolve this circular import issue?
Hi may be this will help , try to remove from authentication.serializers import UserBaseSerializer from multi_users.serializers because when you try to access
from multi_users.serializers import AccessLevelPermissionSerializer
permissions = AccessLevelPermissionSerializer(required=False)
this code from authentication.serializers, you being in authentication.serializers try to access from multi_users.serializers import AccessLevelPermissionSerializer
which will redirect your call to multi_users.serializers where you are again importing from authentication.serializers import UserBaseSerializer which making it circular being in that file you again access another file which indirectly calling same file so please try to remove from authentication.serializers import UserBaseSerializer from multi_users.serializers
Related
I am trying to get users details from django using rest framework. but there is error:
module 'core.model' has no attribute 'Users'
to do that I added this line in settings.py:
REST_AUTH_SERIALIZERS = { 'USER_DETAILS_SERIALIZER':'users.serializers.userSerializer' }
Since it is a model of Django auth and in my models.py there is no model of such users it is from Django auth model and I don't know how to access the data from it.
from rest_framework import serializers
from core import models
class userSerializer(serializers.ModelSerializer):
class Meta:
fields=(
'id',
'username')
model=models.Users
Here is the screenshot of my django admin:
We must import User model like:
from django.contrib.auth.models import User
It seems that you have a custom User model inside the core app.
Be sure you have substituted your User model inside settings.py:
AUTH_USER_MODEL = 'myapp.MyUser'
Would be 'core.User' in your case, I believe.
Then to reference your custom user model you can possibly:
from rest_framework import serializers
from django.conf import settings
from core import models
class userSerializer(serializers.ModelSerializer):
class Meta:
fields=(
'id',
'username')
model=settings.AUTH_USER_MODEL
or
from rest_framework import serializers
from django.contrib.auth import get_user_model
from core import models
class userSerializer(serializers.ModelSerializer):
class Meta:
fields=(
'id',
'username')
model=get_user_model()
I have 2 different application in python. I want to import model and use it to make serializer function in another app. Here is my code:
from django.contrib.auth import models as auth_models
from . import models as client_models
from crm.models import models as crm_models
from rest_framework import serializers
class Capability(serializers.ModelSerializer):
class Meta:
model = crm_models.Capability
fields = ["id", "name"]
class Client(serializers.ModelSerializer):
industry = Industry(read_only=True)
capability = Capability(read_only=True)
class Meta:
model = client_models.Client
fields = [
"id",
"company",
"entity",
"account_status",
"capability"]
Here I am getting error for,
in Meta
model = crm_models.Capability
AttributeError: module 'django.db.models' has no attribute 'Capability'
Without seeing the file structure I'd take a guess that you want to change your import to:
from crm import models as crm_models
as I think you are looking for models in models currently.
Try to use this :-
from crm.models import Capability
class Capability(serializers.ModelSerializer):
class Meta:
model = Capability
fields = ["id", "name"]
This is my forms.py file!!!
from django import forms
from . models import URLModel
class URLForm(forms.ModelForm):
class Meta:
model = 'URLModel'
fields = ['url']
This is my models.py file
from django.db import models
# Create your models here.
class URLModel(models.Model):
url=models.URLField(unique=True)
short=models.CharField(max_length=200,unique=True)
def __str__(self):
return self.url
Now i just wanted to ask that why this error is coming and i have revieved and i cant observe any error for real...please help
# Remove single quote from your model specification in forms.py
from django import forms
from .models import URLModel
class URLForm(forms.ModelForm):
class Meta:
model = URLModel
fields = ('url',)
I am trying to customize djoser's createuser end point. For that I have installed django custom user following this link https://github.com/jcugat/django-custom-user. Here is my models.py
from django.db import models
from custom_user.models import AbstractBaseUser
class Profile(AbstractBaseUser):
account_address = models.CharField(max_length=30, blank=True)
and serializers.py
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
fields = ('url', 'id', 'email', 'first_name', 'account_address', 'password')
and in app.admin.py i have registered it in following way.
from django.contrib import admin
from custom_user.admin import UserAdmin
from .models import Profile
class MyCustomEmailUserAdmin(UserAdmin):
"""
You can customize the interface of your model here.
"""
pass
# Register your models here.
admin.site.register(Profile, UserAdmin)
but when I am trying to makemigrations i am running into following error.
any clue what's wrong here?
From the looks of it, you seem to have the following in your settings:
AUTH_USER_MODEL = "your_app.Profile"
And for your profile model, you are inheriting from AbstractBaseUser which is actually from from django.contrib.auth.models import AbstractBaseUser.
I believe you meant to inherit from AbstractEmailUser. Thus, your "Profile" model would actually need be:
from custom_user.models import AbstractEmailUser
class Profile(AbstractEmailUser):
account_address = models.CharField(max_length=30, blank=True)
I use django-rest-auth in my Django project.
After login the rest-auth/login/, how to return more information?
In the rest-auth/login/, when I login the user, it returns a key.
I want to also return the user's information, how can I get this?
At last, I get my solution:
class TokenSerializer(serializers.ModelSerializer):
"""
Serializer for Token model.
"""
user = UserInfoSerializer(many=False, read_only=True) # this is add by myself.
class Meta:
model = TokenModel
fields = ('key', 'user') # there I add the `user` field ( this is my need data ).
In the project settings.py, add the TOKEN_SERIALIZER like bellow:
REST_AUTH_SERIALIZERS = {
...
'TOKEN_SERIALIZER': 'Project.path.to.TokenSerializer',
}
Now I get my need data:
Please refer to this link
You can override the default TokenSerializer with a custom serializer that will include users.
in a file say yourapp/serializers.py
from django.conf import settings
from rest_framework import serializers
from rest_auth.models import TokenModel
from rest_auth.utils import import_callable
from rest_auth.serializers import UserDetailsSerializer as DefaultUserDetailsSerializer
# This is to allow you to override the UserDetailsSerializer at any time.
# If you're sure you won't, you can skip this and use DefaultUserDetailsSerializer directly
rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
UserDetailsSerializer = import_callable(
rest_auth_serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer)
)
class CustomTokenSerializer(serializers.ModelSerializer):
user = UserDetailsSerializer(read_only=True)
class Meta:
model = TokenModel
fields = ('key', 'user', )
and in your app settings use rest-auth configuration to override the default class
yourapp/settings.py
REST_AUTH_SERIALIZERS = {
'TOKEN_SERIALIZER': 'yourapp.serializers.CustomTokenSerializer' # import path to CustomTokenSerializer defined above.
}