I am trying to update the user profile in django rest framework
So i am able to create the user profile simultaneously with user creation using signals
Now i am trying to update the profile created:-
//models.py
class User(AbstractBaseUser,PermissionsMixin):
phone_regex=RegexValidator(regex = r'^[6-9]\d{9}$',message='please enter the correct phonenumber')
#name_regex=RegexValidator(regex=r'/^[A-Za-z]+$/',message='Please enter the correct name')
phone=models.CharField(validators=[phone_regex],max_length=15,unique=True)
date_joined=models.DateTimeField(verbose_name='date joined',auto_now_add=True)
last_login=models.DateTimeField(verbose_name='last login',auto_now=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False)
first_login=models.BooleanField(default=True)
USERNAME_FIELD='phone'
REQUIRED_FIELDS = []
class UserProfile(models.Model):
# There is an inherent relationship between the Profile and
# User models. By creating a one-to-one relationship between the two, we
# are formalizing this relationship. Every user will have one -- and only
# one -- related Profile model.
GENDER = (
('M', 'Homme'),
('F', 'Femme'),
)
user = models.OneToOneField(
User,related_name="profile", on_delete=models.CASCADE
)
# Each user profile will have a field where they can tell other users
# something about themselves. This field will be empty when the user
# creates their account, so we specify blank=True.
email=models.EmailField(unique=True,validators=[validate_email],max_length=254,blank=True,null=True)
name=models.CharField(max_length=15,blank=True)
dateofbirth=models.DateField(auto_now=False, null=True, blank=True)
Gender=models.CharField(max_length=1, choices=GENDER,blank=True,null=True)
address1 = models.CharField(
"Address",
max_length=1024,
blank=True,
null=True
)
address2 = models.CharField(
"Society",
max_length=1024,
blank=True,
null=True
)
address3 = models.CharField(
"Landmark",
max_length=1024,
blank=True,
null=True
)
zip_code = models.CharField(
"ZIP / Postal code",
max_length=12,
blank=True,
null=True
)
city = models.CharField(
"City",
max_length=1024,
default ="Gurugram",
blank=True,
null=True
)
country = models.CharField(
"Country",
max_length=10,
default="India",
blank=True,
null=True
)
# In addition to the `bio` field, each user may have a profile image or
# avatar. This field is not required and it may be blank.
# A timestamp representing when this object was created.
created_at = models.DateTimeField(auto_now_add=True,blank=True)
# A timestamp representing when this object was last updated.
updated_at = models.DateTimeField(auto_now=True,blank=True)
def __str__(self):
return self.user.phone
#receiver(post_save, sender=User)
def create_profile_for_user(sender, instance=None, created=False, **kargs):
if created:
UserProfile.objects.get_or_create(user=instance)
// Serializer.py
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields=("name",)
class UserSerializer(serializers.ModelSerializer):
user = UserProfileSerializer(many=True)
class Meta:
model = User
fields = ('user', 'phone',)
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(**user_data)
profile = user_data.objects.create(user=user, **validated_data)
return profile
def update(self, instance, validated_data):
print("update function")
# profile_data = validated_data.pop('profile')
# print(profile_data)
profile_data = validated_data.pop('user')
profile = instance.profile
# print("profile is ",profile)
print(profile_data.name)
print("name in validation ",validated_data['name'], " type is ",print(type(validated_data)))
instance.username = validated_data.get('name', instance.username)
instance.email = validated_data.get('email', instance.email)
instance.save()
print("name is ",instance.profile.name )
#profile.Gender = validated_data['Gender'],
#profile.email = validated_data['email'],
#profile.dateofbirth=validated_data['dateofbirth'],
#profile.address1=validated_data['address1'],
##profile.address2=validated_data['address2'],
#profile.address3=validated_data['address3']
# print("1",profile)
#print(profile.Gender)
#print(profile.email)
#print(profile.dateofbirth)
profile.save()
print("2",profile)
print(profile.name)
print("save done")
return instance
//views.py
class ProfileCreateAPIView(CreateAPIView):
def put(self, request, *args, **kwargs):
print("my print 0",request)
header_token = request.META.get('HTTP_AUTHORIZATION', None)
print(header_token)
access_token=header_token.split(' ')[1]
status,user =validate_token(access_token)
print(request.data)
user_profile_serializer = UserProfileSerializer(
instance=user,
data=request.data
)
user_serializer = UserSerializer(
instance=user,
data=request.data)
print("my print 1",user_profile_serializer.is_valid())
print("my print 2",user_serializer.is_valid())
if user_profile_serializer.is_valid() and user_serializer.is_valid():
user_profile_serializer.save()
print("user_profile save done")
user_serializer.save()
print("user save done")
return Response(
status=rt_status.HTTP_200_OK)
# Combine errors from both serializers.
# errors = dict()
# errors.update(user_profile_serializer.errors)
#errors.update(user_serializer.errors)
else:
return Response(status=rt_status.HTTP_400_BAD_REQUEST)
'''
return Response(status=rt_status.HTTP_400_BAD_REQUEST)
'''
Now the issue is when i am hitting the api I am getting user_serializer.is_valid() as TRUE but False from UserProfile.
I feel there is some issue with my getting the profile data.
But I am not able to understand how to resolve the issue .
Any help is much appreciated.
Related
I have a child inlineformset that saves if it has been changed by the user, but does not save the default value if left unchanged.
SeVsEff is the child, and patient is the parent
models.py
class Patient(TimeStampedModel):
patient_id = models.UUIDField(
primary_key=True, unique=True, default=uuid.uuid4, editable=False
)
name = models.CharField("Patient Name", max_length=255)
user = models.ForeignKey(
settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL
)
class SeVsEff(TimeStampedModel):
value = models.IntegerField(default=20)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
forms.py
class PatientForm(ModelForm):
class Meta:
model = Patient
fields = ["name"]
SevseffFormSet = inlineformset_factory(
Patient,
SeVsEff,
fields=("value",),
widgets={'value': RangeInput()},
extra=0,
min_num=1,
validate_min=True,
labels=None,
)
views.py
def post(self, *args, **kwargs):
form = PatientForm(data=self.request.POST)
sevseff_formset = SevseffFormSet(data=self.request.POST)
if form.is_valid():
patient_instance = form.save()
patient_instance.user = self.request.user
patient_instance.save()
if sevseff_formset.is_valid():
sevseff_name = sevseff_formset.save(commit=False)
for sevseff in sevseff_name:
sevseff.patient = patient_instance
sevseff.save()
So I think the issues is that the sevseff_formset is not registered as valid unless it is changed, but if I add something like:
if not sevseff_formset.has_changed():
sevseff_name = sevseff_formset.save(commit=False)
for sevseff in sevseff_name:
sevseff.patient = patient_instance
sevseff.save()
This doesn't work as sevseff_name is empty.
This does it:
sevseff_name = sevseff_formset.save(commit=False)
for sevseff in sevseff_name:
sevseff.patient = patient_instance
sevseff.save()
if not sevseff_name:
SeVsEff(value=20, patient=patient_instance).save()
Solution from https://forum.djangoproject.com/t/django-inline-formset-not-saving-default-values/10647/3
I trying to update the user profile but I can't get that what mistake I do here.
#forms.py
class AddStudentUserForm(forms.ModelForm):
class Meta:
model = User
fields = ["username", "first_name", "last_name"]
class AddStudentStaffForm(forms.ModelForm):
class Meta:
model = Staff
fields = ["gender", "dob", "mob", "img"]
#views.py
def editProfile(request, user, id):
if request.POST:
staffform = AddStudentStaffForm(request.POST or None, request.FILES or None, instance=request.user.staff_profile)
userform = AddStudentUserForm(data=request.POST, instance=request.user)
if staffform.is_valid():
staffform.save()
if userform.is_valid():
userform.save()
return redirect(profile,user)
return render(request, 'edit.html')
#models.py
class Staff(models.Model):
Male = 'Male'
Female = 'Female'
Other = 'Other'
GENDER_CHOICE = (
(Male, 'Male'),
(Female, 'Female'),
(Other, 'Other')
)
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(User, related_name='staff_profile', on_delete=models.CASCADE)
gender = models.CharField(max_length=10, choices=GENDER_CHOICE, default=Male)
dob = models.DateField(auto_now_add=False, blank=True, null=True, default=None)
mob = models.IntegerField(blank=True, null=True, default=None)
img = models.ImageField(upload_to='staff/', blank="True", null="True")
objects = models.Manager()
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Staff.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.staff_profile.save()
def __str__(self):
return self.gender
I started to create a Student Management System project. But I faced some difficulties to develop it. I trying lot of to solve these problem but since I can't solve that.
Here if I want to update the Staff model fields like gender, dob, mob it will updated successfully but when I trying to update the User model fields like username, first_name, last_name it will not update. I think the if userform.is_valid(): condition couldn't run, but I can't get what mistake I do here.
You can debug why the form is not valid, try code below :
if userform.is_valid():
userform.save()
return redirect(profile,user)
else:
print userform.errors #To see the form errors in the console.
I have a webapp where we can create communities with django as a backend, but when i try to send a POST to create a community, I get:
community_configuration: ["Incorrect type. Expected pk value, received str."]
My POST:
{
title: knkn kn .k jbjnmn,
logo: [object File],
is_active: true,
description: test,
welcome_message: Welcome message,
org_id: 114,
community_configuration: About us,Community news,FAQs,Supporters,Resources,
}
Here are my serializers:
class MicroConfigurationSerializer(serializers.ModelSerializer):
class Meta:
model = MicroConfiguration
fields = [
'name',
]
def to_representation(self, instance):
return instance.name
class CommunityConfigurationSerializer(serializers.ModelSerializer):
micro_configurations = MicroConfigurationSerializer(many=True, read_only=True)
class Meta:
model = CommunityConfiguration
fields = '__all__'
class CommunitySerializer(serializers.ModelSerializer):
logo = serializers.SerializerMethodField()
organisation = serializers.SerializerMethodField()
community_configuration = CommunityConfigurationSerializer()
class Meta:
model = Community
fields = (
...
etcetc
...
)
Heres my model:
class MicroConfiguration(core_models.BaseModel):
name = models.CharField(max_length=32, unique=True)
permissions = models.ManyToManyField(Permission)
def __str__(self):
return self.name
class CommunityConfiguration(core_models.BaseModel):
name = models.CharField(max_length=32)
micro_configurations = models.ManyToManyField(MicroConfiguration)
permission_group = models.ForeignKey(
Group,
on_delete=models.SET_NULL,
null=True
)
def __str__(self):
return self.name
class Community(core_models.BaseModel):
accounts = models.ManyToManyField(
settings.AUTH_USER_MODEL,
through='associates.AccountCommunity',
through_fields=('community', 'account')
)
goals = models.ManyToManyField(Goal, through='associates.CommunityGoal')
type = models.ManyToManyField(CommunityType, through='CommunityAttribute')
communities = models.ManyToManyField(
'self',
through='associates.CommunityCommunity',
symmetrical=False,
)
crossposts = models.ManyToManyField(
Action,
through='activities.CommunityCrosspost',
through_fields=('community', 'action'),
related_name='communities',
)
title = models.CharField(max_length=64)
logo = models.ImageField(null=True, blank=True)
intro_media = models.ForeignKey(
'associates.MediaStore',
null=True,
on_delete=models.SET_NULL
)
website_url = models.CharField(max_length=256, blank=True)
is_invite_only = models.BooleanField(default=False)
description = models.TextField(blank=True)
intro_message = models.TextField(blank=True)
welcome_message = models.TextField(blank=True)
signup_autojoin = models.BooleanField(default=False)
community_configuration = models.ForeignKey(
CommunityConfiguration,
default=1,
null=True,
on_delete=models.SET_NULL,
help_text='Do not edit directly, create a new custom config instead, \
as it is reference by difference community.',
)
objects = models.Manager()
associates = AssociationManager()
#property
def url(self):
return reverse(
'communities:detail',
kwargs={
'id': self.id,
}
)
class Meta:
verbose_name = 'Community'
verbose_name_plural = 'Communities'
def __str__(self):
return self.title
Here's my views:
class CommunityCreateAPIView(CreateAPIView):
serializer_class = CommunityCreateSerializer
def create(self, request, **kwargs):
organisation_id = request.data.get('org_id')
micro_configurations_qd = request.data.copy()
micro_configurations = micro_configurations_qd.pop('community_configuration', False)
if micro_configurations:
micro_configurations = micro_configurations[0].split(',')
user = request.user
data = {}
permitted = AccountOrganisation.objects.filter(
account=user,
organisation_id=organisation_id,
is_active=True,
association__permissions__codename='add_community',
).exists()
if not permitted and not request.user.is_superuser:
data['message'] = 'Action not allowed'
return Response(
data=data,
status=status.HTTP_400_BAD_REQUEST,
)
response = super().create(request, **kwargs)
if response.status_code == 400:
data['message'] = 'Failed to update community'
return Response(
data=data,
status=status.HTTP_400_BAD_REQUEST,
)
community_id = response.data.get('id')
OrganisationCommunity.objects.create(
community_id=community_id,
organisation_id=organisation_id,
)
association = Group.objects.get(name='Community Admin')
AccountCommunity.objects.create(
community_id=community_id,
account=user,
association=association,
)
if micro_configurations:
com_config_qs = CommunityConfiguration.objects.filter(
micro_configurations__name__in=micro_configurations
).annotate(
num_micro_config=Count('micro_configurations__name')
).filter(
num_micro_config=len(micro_configurations)
)
community_configuration = None
if micro_configurations:
for com_config in com_config_qs:
micro_config_count = com_config.micro_configurations.count()
if micro_config_count == len(micro_configurations):
community_configuration = com_config
break
if community_configuration:
Community.objects.filter(
pk=community_id
).update(
community_configuration=community_configuration
)
elif micro_configurations:
micro_qs = MicroConfiguration.objects.filter(
name__in=micro_configurations
)
community_config = CommunityConfiguration.objects.create(
name='Custom'
)
community_config.micro_configurations.set(micro_qs)
community_config.save()
Community.objects.filter(
pk=community_id
).update(
community_configuration=community_config
)
return response
I've been stuck with this for hours, any help?
Your serializer expects a pk (or id) of the community_configuration instance.
basically you have it set up so that you need to create a community_configuration entry first, then fetch the id of the new created entry and use that id when creating your community.
If you want to have the community_configuration instance to be created while creating the community then an option to do that would be to override the create method in the serializer and extract the community_configuration data then create a new entry for that model and use it, something like this
class CommunitySerializer(serializers.ModelSerializer):
# .. put your serializer code here
def create(self, validated_data):
community_configuration= validated_data.pop('community_configuration')
config_instance, created = CommunityConfiguration.objects.get_or_create(<community_configuration>) # modify this however you need to create the CommunityConfiguration instance
community_instance = community.objects.create(**validated_data, community_configuration=config_instance)
return community_instance
you will need to probably modify the code for it to follow your needs.
I didn't read all of your models, not sure how you are gonna create the nested models from the input value but you get the point.
I have problem with saving instance Live_In Nested Serializers in Django Rest Framework. Hope your guys help me! I think just a basic issue.
My Serializers:
I think it comes error when I write saving instance
class CityLiveInSerializer(ModelSerializer):
country = CharField(required=False, source='country.name')
class Meta:
model = City
fields = [
'name',
'slug',
'country'
]
class UserEditSerializer(ModelSerializer):
live_in = CityLiveInSerializer(source='profile.live_in')
about = serializers.CharField(source='profile.about')
class Meta:
model = User
fields = [
'username',
'live_in',
'about',
]
def update(self, instance, validated_data):
instance.username = validated_data.get('username', instance.username)
instance.save()
# Update Serializers Profile
if (validated_data.get('profile') is not None):
profile_data = validated_data.pop('profile')
profile = instance.profile
profile.about = profile_data.get('about', profile.about)
profile.save()
if (validated_data.get('live_in') is not None):
live_in_data = validated_data.pop('live_in')
try:
city = City.objects.get(name=live_in_data['name'])
except City.DoesNotExist:
city = City.objects.create(**live_in_data)
instance.profile.live_in = city
instance.profile.save()
return instance
My City Model (Live_in)
class City(BaseCity):
class Meta(BaseCity.Meta):
swappable = swapper.swappable_setting('cities', 'City')
class BaseCity(Place, SlugModel):
name = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
country = models.ForeignKey(swapper.get_model_name('cities', 'Country'), related_name='cities', null=True, blank=True)
My Profile Model
class Profile(models.Model):
# Extend User
user = models.OneToOneField(User, unique=True)
about = models.TextField(max_length=1000, default='', blank=True, null=True)
live_in = models.ForeignKey(City, null=True, blank=True, related_name="live_in")
Data sent by Postman (Json)
{ "live_in": { "name": "Encamp" } }
TraceError:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in data
263. self._data = self.to_representation(self.instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
488. attribute = field.get_attribute(instance)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/fields.py" in get_attribute
463. raise type(exc)(msg)
Exception Type: AttributeError at /api/v1/users/account/edit/
Exception Value: Got AttributeError when attempting to get a value for field live_in on serializer UserEditSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the User instance.
Original exception text was: 'User' object has no attribute 'live_in'.
First of all you dont need many=True in this case. It required for related objects list, but you will pass only one city.
Secondly live_in is attribute of profile model so you need to update profile and add source argument:
live_in = CityLiveInSerializer(source="profile.live_in")
def update(self, instance, validated_data):
instance.username = validated_data.get('username', instance.username)
instance.save()
# Update Serializers Profile
if (validated_data.get('profile') is not None):
profile_data = validated_data.pop('profile')
profile = instance.profile
profile.about = profile_data.get('about', profile.about)
if (profile_data.get('live_in') is not None):
live_in_data = profile_data.pop('live_in')
try:
city = City.objects.get(name=live_in_data["name"])
except City.DoesNotExist:
city = City.objects.create(**live_in_data)
profile.live_in = city
profile.save()
return instance
In this case you need to allow create city without country, so you need to add null=True, blank=True to country attribute:
class BaseCity(Place, SlugModel):
name = models.CharField(max_length=200, db_index=True, verbose_name="standard name")
country = models.ForeignKey(swapper.get_model_name('cities', 'Country'),
related_name='cities', null=True, blank=True)
i have my models and everything like this, when i'm trying to upload a Image this is what i got:
'PhotoSerializer' object has no attribute '_committed
My view:
class UploadPhoto(APIView):
#authentication_classes = (TokenAuthentication,)
permission_classes = ()#(IsAuthenticated,)
def put(self, request, username):
user = User.objects.get(username = username)
userprofile = UserProfile.objects.get(user= user)
photo = PhotoSerializer(data = request.data)
userprofile.photo = photo
userprofile.save()
return Response(status= status.HTTP_200_OK)
Serializer:
class PhotoSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = (
'photo',
)
And at least my Model.py
class UserProfile(models.Model):
"""
User Profile
"""
user = models.OneToOneField(User, related_name='userprofile')
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',message="Phone must be entered in the format: '+999999999'. Up 15 digits allowed.")
#The Field on DataBase after check if it's a valid Phone Number.
# validators should be a list
phone_number = models.CharField(validators=[phone_regex], max_length=15, blank=True)
photo = models.ImageField(upload_to = 'photos/', null = True)
driver_passenger = models.BooleanField(default=True)
rides_given = models.IntegerField(default=0)
rides_taken = models.IntegerField(default=0)
reputation = models.IntegerField(default=0)
I tried some options to upload a photo, but I couldn't implemented so I appreciate your help (:
Well, after a little of trying I Could, upload the Image With this
code hope is useful
View.py
class UploadPhoto(APIView):
#authentication_classes = (TokenAuthentication,)
permission_classes = ()#(IsAuthenticated,)
def put(self, request, username):
user = User.objects.get(username = username)
userprofile = UserProfile.objects.get(user= user)
photo = PhotoSerializer(userprofile,data = request.data)
photo.is_valid(raise_exception=True)
photo.save()
return Response(status= status.HTTP_200_OK)