Issue
I have a custom user model with subclassing AbstractUser I want to update it with an Update view
models.py:
class UserManager(models.Manager):
def New_Requests(self):
return super.get_queryset().filter(is_seller="I")
class User(AbstractUser):
nickname = models.CharField(max_length=50,verbose_name="Nick Name",default='User')
is_seller_status = (
('N','Not accepted'),
('I','Investigate'),
('A','Accepted')
)
is_seller = models.CharField(default='N',max_length=1,choices=is_seller_status,verbose_name='seller')
user_id = models.UUIDField(default = uuid.uuid4,editable = False,unique=True)
profile = models.ImageField(upload_to="user_profile",blank=True,null=True)
admin_reject_reason = models.TextField(default='Not reviewed yet')
views.py:
class AccountView(LoginRequiredMixin,UpdateView):
model = User
form_class = UserProfileForm
template_name = "user/profile.html"
success_url = reverse_lazy("user:profile")
def get_object(self):
return User.objects.get(pk = self.request.user.pk)
def get_form_kwargs(self):
kwargs = super(AccountView, self).get_form_kwargs()
kwargs['user'] = self.request.user # Pass 'user' directly to the form
return kwargs
forms.py:
class UserProfileForm(UserChangeForm):
#User profileform
def __init__(self,*args, **kwargs):
user = kwargs.pop('user')
super(UserProfileForm, self).__init__(*args, **kwargs)
if not user.is_superuser:
self.fields['first_name'].disabled = True
self.fields['last_name'].disabled = True
#self.fields['email'].help_text = "Change it if it was neccessary"
self.fields['email'].disabled = True
self.fields['is_seller'].disabled = True
class Meta:
#specifing the model and fields
model = User
fields = ['profile','nickname','username','email','first_name','last_name',
'is_seller']
urls.py:
app_name = "user"
urlpatterns = [
...
path('profile/',AccountView.as_view(),name='profile'),
...
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)`
profile.html:
{% extends 'user/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Profile</h1>
</div>
<div class="col-md-12">
<div class = "card card-primary">
<div class="card-header">
<h3 class = "card-title mb-0 float-left">User Update</h3>
</div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.username|as_crispy_field }}
</div>
<div class="col-6">
{{ form.email|as_crispy_field }}
</div>
<div class="col-6">
{{ form.first_name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.last_name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.is_seller|as_crispy_field }}
</div>
<div class="col-6">
{{ form.profile|as_crispy_field }}
</div>
</div>
<input class="btn btn-success" type="submit" value="Update">
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
<!-- End of Main Content -->
{% endblock %}
settings.py:
AUTH_USER_MODEL = "user.User"
AUTH_PROFILE_MODULE = "user.User"
and I have the app installed
When ever I hit the update button it reloades the page and the changes apply in the template not in database If you open a page the information that it display's is not updated I'm using django 4.2.8 btw
Solution
You've set the nickname in form fields but you don't use it inside the template.
You haven't set blank=True
for the nickname
model field, so when you send the form data without nickname value you get "This field is required"
error for that field. (you can check this by overriding form_invalid
method and printing errors)
You can do one of these:
1- add blank=True
in your nickname model field
2- add nickname field inside your template
3- just delete it from your form fields if you don't need to it
Answered By - Rauf Masoumi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.