How To Update Existing Profile Django Model Form - django

I am trying to allow users to be able to create and edit their profiles once they have registered. I am using a model form. What I need to do is have the employer model field be filled with the current user.
Here is my view:
def update_profile(request, username):
if request.method == 'POST':
edit_profile_form=EditProfileForm(request.POST)
if edit_profile_form.is_valid():
editprofile = edit_profile_form.save(commit=False)
editprofile.employer = request.user.get_profile()
editprofile.save()
edit_profile_form = EditProfileForm()
context = {'edit_profile_form':edit_profile_form,}
return render(request, 'pandaboard/editprofile.html', context)
Here is my model:
class Profile(models.Model):
employer = models.ForeignKey(User)
company_name = models.CharField(max_length=100)
company_description = models.TextField()
company_website = models.URLField(max_length=200, blank=True)
contact_email = models.EmailField(max_length=100)
contact_name = models.CharField(max_length=100)
def __unicode__(self):
return self.company_name
Here is my Model Form
from django.forms import ModelForm
from pandaboard.models import JobPost, Profile
from django.contrib.auth.models import User
class EditProfileForm(ModelForm):
class Meta:
model = Profile
fields = ['company_name','company_description','company_website','contact_email','contact_name']

To hydrate your form with values from your existing model instance, you need to use the instance argument on the model form:
def update_profile(request, username):
profile = request.user.get_profile()
edit_profile_form = EditProfileForm(request.POST or None,
current_user=request.user, instance=profile)
if request.method == 'POST':
if edit_profile_form.is_valid():
editprofile.save()
context = {'edit_profile_form': edit_profile_form}
return render(request, 'pandaboard/editprofile.html', context)
To inject the current request.user, you can override the __init__ of EditProfileForm, passing in an extra keyword argument (or arg, it doesn't really matter), and the popping it out of the kwargs before calling super so you aren't passing the ModelForm a keyword argument it isn't expecting:
class EditProfileForm(ModelForm):
class Meta:
model = Profile
def __init__(self, *args, **kwargs):
current_user = kwargs.pop('current_user')
super(EditProfileForm, self).__init__(*args, **kwargs)
self.fields['employer'] = current_user
Now you don't have to pass commit=False and manually set the value of employer in the view.

Related

How to Query a ForeignKey field pointing to another ForeignKey field in Django

I have a model (Letter) with a foreign key, pointing to another model (Company) with a foreign key. Below is a simple schema from models.py
from django.contrib.auth.models import User
class Company (models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, editable=False)
date_created = models.DateField(default=timezone.now, null=True)
company_name = models.CharField(max_length=100, null=True)
class Letter(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='letterhead_user', null=True)
company = models.ForeignKey(Company, related_name = "company_letter", on_delete=models.CASCADE, null=True)
subject = models.CharField(max_length=5000, null=True)
body = models.TextField()
I have created a form where users can create Letters with the model through ModelForm.
class LetterForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(LetterForm, self).__init__(*args, **kwargs)
self.fields['company'].widget.attrs = {'class': 'input',}
self.fields['subject'].widget.attrs = {'class': 'input', 'placeholder': 'RE: ...'}
self.fields['body'].widget.attrs = {'class': 'textarea',}
class Meta:
model = Letter
fields = ('company', 'subject', 'body',)
The View:
def letter_form (request):
form = LetterForm()
if request.method == 'POST':
form = LetterForm(request.POST, request.FILES)
form.instance.user = request.user
if form.is_valid():
form.save()
return redirect('letter')
Currently, when the user is presented with a form to create a Letter, on the Company field, all the companies from all the users appear. See the pic below:
Front end Form
I would like only the companies that the User has created to appear in the drop-down, not all companies from all users. Or to be able to select the first company that the User has created.
You can specify the logged in user in the form and filter accordingly. In the constructor of the form we thus limit the queryset with:
class LetterForm(forms.ModelForm):
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self.fields['company'].widget.attrs = {'class': 'input',}
self.fields['subject'].widget.attrs = {'class': 'input', 'placeholder': 'RE: …'}
self.fields['body'].widget.attrs = {'class': 'textarea',}
if user is not None:
self.fields['company'].queryset = Company.objects.filter(user=user)
# …
and in the view, we then pass the logged in user to the constructor of the form:
from django.contrib.auth.decorators import login_required
#login_required
def letter_form(request):
if request.method == 'POST':
form = LetterForm(request.POST, request.FILES, user=request.user)
form.instance.user = request.user
if form.is_valid():
form.save()
return redirect('letter')
else:
form = LetterForm(user=request.user)
# …
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
If you use a ModelForm for Letterit may look like this:
class LetterForm(forms.ModelForm):
class Meta:
model = Letter
fields = ["company", "subject", "body"]
# you need to init the form with the right user instance
def __init__(self, user=None, *args, **kwargs):
# call the default __init__ behavior
super().__init__(*args, **kwargs)
# this is the trick, you will filter the companies queryset here
if user:
self.fields['company'].queryset = Company.objects.filter(user=user)
So you need to pass down the user in the form from your view:
something like:
def my_view(request):
# assuming the user follow the standard Django user implementation
# and you user is logged in
form = LetterForm(request.POST or None, user=request.user)
if form.is_valid():
form.save()
# then redirect the user to whatever success page
# render the form
return render(request, "your_template.html", {"form": form})

ModelForm inserts number in foreign key field

I have model from which I created a ModelForm:
models.py:
class City(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return f'{self.name}'
class Profile(models.Profile):
name = models.CharField(max_length=50)
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=False)
location = models.ForeignKey('City', on_delete=models.SET_NULL, blank=True, null=True)
forms.py
from django import forms
from .models import Profile, City
class LocationField(forms.CharField):
def clean(self, value):
try:
city = City.objects.get(name=value)
except ObjectDoesNotExist:
city = City.objects.create(name=value)
return city
class ProfileForm(forms.ModelForm):
location = LocationField()
class Meta:
model = Profile
exclude = ['user']
views.py
def profile_update_view(request):
template_name = 'profiles/update.html'
user = request.user
profile = Profile.objects.get(user__id=user.id)
if request.method == 'GET':
form = ProfileForm(instance=profile)
else:
form = ProfileForm(request.POST, instance=profile)
if form.is_valid():
obj = form.save(commit=False)
obj.user = user
obj.save()
return redirect('profile_view')
context = {'form': form}
return render(request, template_name, context=context)
When I'm saving form, I'm satisfied how it's working, but when I load form again to update in, it fills LocationField() as an City pk integer, but I want it to load name instead. Is there a way to do this?
I've added in views.py:
if request.method == 'GET':
initial = {}
if profile.location:
initial = {'location': profile.location.name}
form = ProfileForm(instance=profile, initial=initial)
now it's working. But it's some workaround. I've thought there is some parameter maybe

Save form data to database

I am new in Django and I really need help,
I do not know how to save my form data to database. I have problem to views.py
I will user's id who filled the form added into the foreign key field.
If there is any link or example that help me I appreciate you.
# views.py
#login_required(login_url="home")
def melk_new(request):
form = MelkForm()
???
return render(request, 'melk_new.html',{'form': form})
# models.py
class Melk(models.Model):
category = models.CharField(max_length=50)
city = models.CharField(max_length=100)
person = models.ForeignKey('CustomUser', on\_delete=models.CASCADE)
def \_\_str\_\_(self):
return self.category
class CustomUser(AbstractUser):
def __str__(self):
return self.email
---------------------------------------------------------------------------
# forms.py
class MelkForm(forms.ModelForm):
class Meta:
model = Melk
fields = ('category', 'city')
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = ('username', 'email')
To get the currently logged in user you'll find it within request.user object. but before you assume that there is a currently logged in user, you need to validate that so you have 2 widely known options:
request.user.is_authenticated()
#login_required() decorator used if you from django.contrib.auth.decorators import login_required
if request.user.is_authenticated():
Car.objects.create(model_id=some_id, person=request.user)
Note:
The #login_required() is added above the function
#login_required(login_url="home")
def melk_new(request):
form = MelkForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user_id = request.user.id
instance.save()
return render(request,'melk_new.html', { 'form': form})

views.py for extended user model by onetoone to add profile information

I am extending User model to add profile information using onetoone relation. The user should be able to change the basic user profile info.
What should be the views to add such functionality?
Here is my model
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
description = models.CharField(max_length=100, default='No description field')
city = models.CharField(max_length=100, default='Location not added')
phone = models.CharField(max_length=10, default='',blank= True)
image = models.ImageField(upload_to='products/profile/%Y%/%m/%d', blank=True)
def __str__(self):
return self.user.username
Here is my form
forms.py
class UserForm(forms.ModelForm):
class Meta():
model = UserProfile
fields = ['description', 'city', 'phone', 'image']
What should I include in my views?
views.py
#login_required()
def edit_profile(request):
pass
I want to update the model the extended UserProfile instance in a way that the previous info gets overwritten and the new information is updated.
You need to handle the UserForm in the view like any other usual form.
here what you need to do:
views.py
def edit_profile(request):
if request.method == 'POST':
# don't forget to pass request.FILES since you have imageField
form = UserForm(request.POST,
request.FILES,
instance=request.user.userprofile)
if form.is_valid():
form.save()
return redirect(reverse('to_user_profile_url'))
else:
form = UserForm(instance=request.user.userprofile)
context = {'form': form}
return render(request, 'edit_profile_template', context)

django override model save() method

I want to get request as a parameter in save() method.
models.py
class Profile(models.Model):
uuid = UUIDField(auto=True)
user = models.ForeignKey(User)
name = models.CharField(max_length=128)
dob = models.DateField()
class Meta:
db_table = 'profile'
def save(self,*args,**kwargs):
if not self.pk:
self.user_id = 2 #here i need request.user instead of 2
super(Profile,self).save(*args,**kwargs)
forms.py
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
exclude = ['uuid','user']
views.py
def add(request):
profile_form = ProfileForm(request.POST)
profile_form.save()
Instead of the value 2 i want to pass request.user. How can i do it. If question is not correct somebody please correct the question.
Don't do that in the model. Do it in the view.
profile_form = ProfileForm(request.POST)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = request.user
profile.save()
One way is to use crequest, like this:
# model
from crequest.middleware import CrequestMiddleware
class Profile(models.Model):
# Some fields
def save(self, *args, **kwargs):
crequest = CrequestMiddleware.get_request() #its the current request
if crequest.user.pk is 1:
# Do stuff
pass
super(Profile,self).save(*args,**kwargs)
You can use kwargs.
For example:
views.py
profile_form.save(your_arg = request)
models.py
your_var = kwargs.pop("your_arg", None) #Second argument of kwargs.pop is default value
In you case, pass request as keyword argument, and then use it in models save() method.