Django access foreignkey fields in a form - python

I'm working on a Virtual Library app (using Django v2.1, python v3.5) where anyone should be able to access the book catalog and request a loan by simply leaving some personal info like name, surname, email, etc.
These are some of the models in models.py:
class Profile(models.Model):
name = models.CharField(max_length=50)
# more fields like surname, email, phone...
class TrackBook(models.Model):
# Somefields to keep track of date and status...
borrower = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True)
class Book(TrackBook):
#info about title, author, etc.
What I'm trying to do is to update a Book instance's borrower with a Profile instance that I created in the Form.
1)I've tried directly accessing borrower fields in a BookForm, but it didn't work.
# views.py
class BookRequestView(UpdateView):
template_name = 'core/book_request.html'
model = Book
form_class = BookProfileForm
#forms.py
class BookProfileForm(forms.ModelForm):
class Meta:
model = Book
fields = ['borrower']
# book_request.html
<form class="" action="" method="post">
{% csrf_token %}
<div class="row">
{{ form.borrower.name }}
<! -- and all other fields -->
</div>
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
<button type="submit" class="btn btn-block btn-success btn-flat">Save</button>
</form>
2) I've tried creating an inlineformset_factory() of Profile model but it doesn't work since what I want to achieve is create a profile form inside the book form, not viceversa. So the example here is not what I'm looking for.
Maybe I'm going out of my mind for a very simple thing, but I can't seem to find any compatible solution for this problem... Any help/suggestion is welcome. Thanks in advance.

You need a form based on Profile, not Book. Your view then needs to create the profile and then set the book's borrower to that.
class BookProfileForm(forms.ModelForm):
book = forms.ModelChoiceField(queryset=Book.objects.all())
class Meta:
model = Profile
fields = ['name', 'address',...]
class BookRequestView(CreateView):
template_name = 'core/book_request.html'
model = Book
form_class = BookProfileForm
def form_valid(self, form):
borrower = form.save()
book = Book.objects.get(self.kwargs['book_id'] # or whatever is in your URL
book.borrower = borrower
book.save()
return redirect(self.get_success_url())

Related

Django - Auto populating Many-To-Many-Field relation

I am trying to make a form that auto populates a many-to-many relation for my user model. The goal is to have a submit button that adds the views instance object (the SingelWorkout object) to a many-to-many field relation within my user model.
The view accurately displays the correct object, and the form appears as intended within the template. I do not wish for the user to see the many-to-many field selection. Aside from the submit button, I am trying to have all logic to occur on the backend. How would I assign an object instance to a field within a form? Would this occur in the views.py or the forms.py?
Here is why my user model looks like:
class FitnessUser(AbstractUser):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(max_length=60)
age_category = models.ForeignKey(
AgeGroup,
on_delete=models.CASCADE,
blank=True,
null=True
)
goal = models.IntegerField(default=1 ,choices=Purpose.choices)
weight = models.CharField(max_length=30)
height = models.CharField(max_length=30)
gender = models.ForeignKey(
Gender,
on_delete=models.CASCADE,
blank=True,
null=True
)
exercise_frequency = models.IntegerField(default=1 ,choices=Frequency.choices)
template_id = models.ForeignKey(
Workout_template,
on_delete=models.CASCADE,
blank=True,
null=True
)
completed_workouts = models.ManyToManyField(SingleWorkout)
def get_absolute_url(self):
return reverse('detail', args=[self.id])
This is my form in forms.py:
class CustomWorkoutChangeForm(UserChangeForm):
class Meta(UserChangeForm):
model = FitnessUser
fields = ('completed_workouts',)
exclude = ('completed_workouts',)
UserChangeForm.password = None
This is how my view looks:
class WorkoutUpdateView(LoginRequiredMixin, UpdateView):
model = SingleWorkout
template_name = 'workout/daily_view.html'
form_class = CustomWorkoutChangeForm
success_url = reverse_lazy("template")
def get_context_data(self, **kwargs):
context = super(WorkoutUpdateView, self).get_context_data(**kwargs)
context['workout'] = SingleWorkout.objects.get(slug = self.kwargs['slug'])
return context
My html template looks like this:
{{workout}}
<br>
workout:
<br>
{{ workout.exercise_1 }}
<br>
{{ workout.description_1 }}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Confirm">
</form>
Figured out a solution. I created a view that gets the instance object based on the objects url slug, and also gets the user by its pk. From there is adds the instance object to the users many to many field, then redirects back to the previous page.
New view created:
def update_workout_data(request, slug):
workout = SingleWorkout.objects.get(slug=slug)
endUser = FitnessUser.objects.get(pk = request.user.pk)
endUser.completed_workouts.add(workout)
endUser.save()
return redirect(reverse('daily', kwargs={'slug':workout.slug}))
Updated HTML appearance. I've also altered the html and its detail view so that the update link will redirect to a separate update view, depending on the need to add/remove the relation.
{% block content %}
Daily View
<br>
{{exercise}}
<br>
workout:
<br>
<br>
{% if exercise.name in workouts %}
<h5>Workout Already Completed</h5>
<form method="POST" action="{% url 'remove' slug=exercise.slug %}">
{% csrf_token %}
<button type="submit">Reset</button>
</form>
{% else %}
<form method="POST" action="{% url 'update' slug=exercise.slug %}">
{% csrf_token %}
<button type="submit">Complete</button>
</form>
{% endif %}
{% endblock content %}
Updated Detail View
def get_context_data(self, **kwargs):
context = super(WorkoutDetailView, self).get_context_data(**kwargs)
user = FitnessUser.objects.get(pk = self.request.user.pk)
context['exercise'] = SingleWorkout.objects.get(slug = self.kwargs['slug'])
context['workouts'] = {}
for workout in user.completed_workouts.all():
context['workouts'][workout.name] = workout
return context

Django: How to autopopulate foreign key with the corresponding model class instance

Working on my first Django project and could use some help. I have 2 models (Decisions, Votes) linked by the foreign key called 'decision'. The template, vote_list.html, shows the user a list of decisions (generated by other users) that are contained in Decisions. The user taps a particular decision and is re-directed to a second template to vote on options pertaining to that decision. How do I autopopulate the foreign key 'decision' in Votes with the corresponding instance of Decision so that the second template, vote_form.html, displays the options for the decision they tapped on? I assume it's coded in views.py (I commented an attempt below that doesn't work), but how might it be done? Thank you!
urls.py
path('vote-list/', views.VoterView.as_view(), name='vote_list'),
path('vote-list/<pk>/vote-form/', views.VoteForm.as_view(), name='vote_form'),
models.py
class Decisions(models.Model):
custom_user = models.ForeignKey(CustomUser,
default=None, null=True,
on_delete=models.SET_NULL)
description = models.CharField(default="",
max_length=100, verbose_name="Decision
Summary")
class Votes(models.Model):
decision = models.ForeignKey(Decisions,
default=None, null=True,
on_delete=models.SET_NULL)
vote = models.CharField(default="", max_length=100,
verbose_name="Your vote")
views.py
class VoteForm(LoginRequiredMixin, CreateView):
model = Votes
form_class = VotingForm
template_name = 'users/vote_form.html'
def post(self, request, *args, **kwargs):
super()
form = self.form_class(data=request.POST)
if form.is_valid():
instance = form.save(commit=False)
# instance.decision = Decisions.description
instance.save()
return redirect('users:vote_list')
forms.py
class VotingForm(forms.ModelForm):
class Meta:
model = Votes
fields = ['vote']
vote_list.html
{% for item in Decisions %}
<tr>
<td>{{ item.description }}</td>
<td><a href="{% url 'users:vote_form' item.id
%}">Vote</a></td>
</tr>
{% endfor %}
vote_form.html
{# trying to display the corresponding
decision description here from vote_list.html # }}
{{ form.vote|as_crispy_field }}
I think this might solve your problem:
Add decision field in the voting form. This will display an option to select for which decision you need to save this Vote for.
If you don't want to allow users to change the Decision, you can mark the field as disabled. See this issue for more details on how to do that. Another alternative is to completely hide the field.
class VotingForm(forms.ModelForm):
class Meta:
model = Votes
fields = ['vote', 'decision']
Add initial value of the decision when instantiating the VotingForm. This will automatically set which decision is selected when displaying the form.
class VoteForm(LoginRequiredMixin, CreateView):
model = Votes
form_class = VotingForm
template_name = 'users/vote_form.html'
# Use this to pass 'pk' to your context in the template
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({'pk': self.kwargs['pk'})
return context
def get_initial(self):
initial = super().get_initial()
initial.update({'decision': self.kwargs['pk']})
return initial
def get_success_url():
# Import reverse from django.urls
return reverse('users:vote_list')
Also, your form should probably be displayed like this in the HTML template: {% crispy form %}. This way all defined fields from the VotingForm class are rendered automatically.
<form method="post" action="{% url 'users:vote_form' pk %}">
{% crispy form %}
</form>

Django how to connect user profile model with comment model for showing data from user profile?

I want to show user profile picture publicly in my blog comment section. I tried to use foreignkey in my comment model for connect user profile model then use this in my html for showing profile picture but didn't work.
<img src="{{blogcomment.userprofile.profile_pic.url}}"> #didn't show any profile picture until I manually go to admin panel and set foreignkey of userprofile in my blogcomment model.
here is my full code:
userprofile model
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name="userprofile")
slug = models.SlugField(max_length=2000,unique=True,blank=True,null=True)
profile_pic = models.ImageField(upload_to='profile/images/',validators=[validate_file_size,FileExtensionValidator( ['png','jpg'] )],blank=True,null=True)
blogcomment model:
class BlogComment(models.Model):
blog = models.ForeignKey(Blog,on_delete=models.CASCADE,null=True, blank=True,related_name="blogcomment_blog")
comment = models.TextField(max_length=50000)
name = models.CharField(max_length=250)
userprofile= models.ForeignKey(UserProfile,on_delete=models.CASCADE,null=True,blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='user_comment',blank=True,null=True)
views.py:
if comment_form.is_valid():
isinstance = comment_form.save(commit=False)
isinstance.user = request.user
isinstance.blog = blog
isinstance.save()
my html template:
{% for q in queryset %}
{{q.user.first_name}}
{{q.comment}}
<img src="{{q.userprofile.profile_pic.url}}">
{%endfor%}
my froms.py
class CommentFrom(forms.ModelForm):
captcha = CaptchaField()
class Meta:
model = BlogComment
fields = ['name','email','comment','parent','sno','blog','user']
my userprofile forms.py
class ProfileFroms(forms.ModelForm):
class Meta:
model = UserProfile
fields = ["profile_pic","mobile","country","website_link","skype","twitter"]
userprofile views.py
class UserProfileUpdate(UpdateView):
model = UserProfile
form_class = ProfileFroms
template_name = 'members/profileupdate.html'
success_url = reverse_lazy('members:user-profile-private')
html template for saving userprofile forms
<form method="POST" enctype="multipart/form-data" runat="server">
{% csrf_token %}
{{form}}
</form>
Finally I solved my problems. As Willem Van Onsem said I am missing somethings in my froms. I need to be save userprofile forignkey with my comment model when any new comment posted. I am using this queryset UserProfile.objects.filter(user=request.user) for find current id then pass this id in forms.
{%for i in user_profile%}
<input type="hidden" name='userprofile' value="{{i.id}}">
{%endfor%}

Filtering displayed objects in django view

I am learning Django. Currently I build my blog project. I want to add function to filter posts by date (you can choose specific date from combo box and click "filter" button and then on the main page will display only this posts which were created in this date). Since I'm still new to django, I'm struggle how to handle it.
My question is how to build a functionality that will extract the sent date from the combo box and pass it to the view where I will do the appropriate filtering in the get_queryset method. Below I publish my code:
Part of my .html file where I build combo box:
<p class='text-muted'>Choose date from the list below.</p>
<form method="GET">
<select name="date_filter">
<option>-----------------</option>
{% for post in posts %}
<option>{{ post.date_posted }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-info btn-sm mt-1 mb-1">Filter</button>
</form>
I would also like each date to be unique and displayed only once. Currently, each date is displayed as many times as many posts were created that day because DateTimeField in my model also stores the post creation hour.
Main page view where post are displayed - in my views.py file:
class PostListView(ListView):
model = Post
template_name = "blog_app/home.html"
context_object_name = 'posts'
ordering = ['-date_posted']
# I believe here should be something which fetch choice from combo box and asign it to the
# date_from_combo_box variable. Please, correct me if I'm wrong.
def get_queryset(self):
# Here I will decide what posts are to be displayed based on the selection made in the combo box
if self.date_posted == date_from_combo_box:
return Post.objects.filter(date_posted=date_from_combo_box)
My models.py file:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Comment(models.Model):
comm_content = models.TextField()
add_date = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
def __str__(self):
return f"Comment of post {self.post} posted at {self.add_date}."
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.post.pk})
Thanks for any hints and advice.
For getting unique date from your post table for dropdown change your query to
Post.objects.dates('date_posted', 'day').distinct()
Change your html
<p class='text-muted'>Choose date from the list below.</p>
<form action="url_to_list_view" method="GET">
<select name="date_filter">
<option>-----------------</option>
{% for post in posts %}
<option>{{ post.date_posted }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-info btn-sm mt-1 mb-1">Filter</button>
</form>
Your listview will look like this.
class PostListView(ListView):
model = Post
template_name = "blog_app/home.html"
context_object_name = 'posts'
ordering = ['-date_posted']
def get_queryset(self):
search = self.request.GET.get('date_filter', None)
if search is not None:
return Post.objects.filter(date_posted__date=search)
else:
return Post.objects.all()
You could use something like this,
Model.objects.filter(date_attribute__month=month, date_attribute__day=day)
or for a range you can use
Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])
Credits

Cannot link my form information with the User

I have a edit page which you can edit your personal information that you register, such as username, first_name, last_name, email. But inside the page i have some extra field such as description, city, website field that you can add/edit into your profile if you wanted to.But after i edit the personal info and fill in the extra field for testing and press the confirm button, the personal information being edit succesfully and there is no error occur. But the problem is when i check the data at Django admin, the UserExtraField model is empty. I hope my explanation is good enough.
the problem is i cant save the extra field to the user that login, but the personal information edit work fine, just the extra field cannot be save to the person. i want the user able to edit their personal profile and also add/edit the extra field if they want to.when they edit their personal profile, i want to add some field so they can have more information in their profile.
there is a picture link at the below too.Thank you.
views.py file
def UserProfileEdit(request):
if request.method == 'POST':
form_edit = EditForm(request.POST, instance= request.user)
form_extra = UserExtra(request.POST,instance=request.user)
if form_edit.is_valid() and form_extra.is_valid():
edit = form_edit.save()
extra = form_extra.save()
extra.user = edit
return redirect('/userprofile/user')
else:
form_edit = EditForm(instance = request.user)
form_extra = UserExtra(instance = request.user)
user_edit = {'form_edit':form_edit,'form_extra':form_extra}
return render(request,'user_profile/user_edit.html',context=user_edit)
forms.py
class EditForm(UserChangeForm):
class Meta():
model = User
fields = ('username', 'first_name', 'last_name', 'email', "password")
#make another forms for extra profile imformation
class UserExtra(forms.ModelForm):
class Meta():
model = UserExtraProfile
fields = ('description','city','website')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
# Create your models here.
class UserExtraProfile(models.Model):
#inherit the User model pk
user = models.OneToOneField(User, on_delete = models.CASCADE)
description = models.CharField(max_length= 250,default= '')
city = models.CharField(max_length=250,default= '')
website = models.URLField(blank= True,default= '')
# image = models.ImageField(upload_to='media/profile_pic', blank=True)
def __str__(self):
return self.user.username
def create_profile(sender,**kwargs):
if kwargs['created']:
user_profile = UserExtraProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender = User)
user_edit.html
{% extends 'base.html'%}
{% load bootstrap3 %}
{% load staticfiles %}
{% block content %}
<div class="container">
<h1>User Profile Edit</h1>
<form method="POST">
{% csrf_token %}
{% bootstrap_form form_edit %}
{% bootstrap_form form_extra %}
<input type="submit" class="btn btn-default" value="Confirm">
</form>
</div>
{% endblock %}
enter image description here
You didn't save the extraprofile after assigning the user. Use commit=False in the form save so you don't hit the db twice.
user = form_edit.save()
extra = form_extra.save(commit=False)
extra.user = user
extra.save()
Also, you need to pass the profile, not the user, to the profile form.
form_extra = UserExtra(request.POST, instance=request.user.userextraprofile)

Categories