Django form in form - django

I wonder if it is possible to make form in form by using django.
For example:
class Category(models.Model):
title = models.CharField(max_length=200)
icon = models.ImageField(upload_to="icons", default="icons/dot.png")
def __str__(self):
return self.title
def get_posts(self):
return Post.objects.filter(category__title=self.title)
class Post(models.Model):
author = models.ForeignKey(Account, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
description = models.CharField(max_length=400)
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
While I was working with forms based on these models I was splitting CategoryForm and PostForm into two forms (category had to be made before post to make user able to choose new category during making new post) and then into two views.
class PostForm(forms.ModelForm):
class Meta:
model = Post
exclude = ('author',)
class CategoryForm(forms.ModelForm):
class Meta:
model = Catergory
fields = '__all__'
def newPostView(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.author = request.user
obj.save()
return redirect('home')
form = PostForm()
context = {'form':form}
return render(request, 'blog/post_form.html', context)
def newCategoryView(request)
...
...
...
I'd like to make one newPost form which will have Category form implemented - I mean I'd like to be make it possible to choose category from already made categories or make new one if its needed without using another view
How can I make it ?

Related

how can i create a cycle of comments and replies?

I wonder how can I make a cycle of comments and replies :
I wanted to make a replyable comment but replies also need to be replyable to make a better communication but I'm just a student and don't have much experience
here is my models :
class Comment(models.Model):
#comments model
post = models.ForeignKey(Post, on_delete=models.CASCADE)
text = models.CharField(max_length=300)
user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
class Meta():
verbose_name_plural = 'Comments'
ordering = ['date']
def __str__(self):
return self.test[:50]
class Reply(models.Model):
#replying to comments
comment = models.ForeignKey(Comment,on_delete=models.CASCADE)
text = models.CharField(max_length=300)
user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
class Meta():
verbose_name_plural = 'Replies'
ordering = ['date']
def __str__(self):
return self.text[:50]
problem is that if i use this models i have to make a new model for every reply and it's not in cycle.
also I tried to check if replyable comment works or not and i got a problem with view:
I couldn't add both forms(comment, reply) in the same get_context_data()
class PostDetailView(FormView, DetailView):
#detail page of items
template_name = 'pages/post_detail.html'
model = Post
form_class = CommentForm, ReplyForm
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['form'] = self.get_form()
return context
def post(self,request, *args, **kwargs):
form = CommentForm(request.POST)
if form.is_valid():
form_instance = form.save(commit=False)
form_instance.user = self.request.user
form_instance.post = self.get_object()
form_instance.save()
return HttpResponseRedirect(self.get_success_url())
else:
super().is_invalid(form)
def get_success_url(self):
return reverse('pages:post_detail',args=(self.kwargs['pk'],))
how can i fix views and make comments and replies both replyable

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

How to add condition on Django model foreign key?

I am new in Django, would you please help me, I have two models, by name of Continent and Country, in the Country form I want to only display the Continents in the dropdown list which their status is true?
models
from django.db import models
from smart_selects.db_fields import GroupedForeignKey, ChainedForeignKey
class Continent(models.Model):
name = models.CharField(max_length=255)
status=models.BooleanField(default=True)
def __str__(self):
return self.name
class Country(models.Model):
continent = models.ForeignKey(Continent, null=True, on_delete=models.SET_NULL)
status=models.BooleanField(default=True)
name = models.CharField(max_length=255)
def __str__(self):
return self.name
forms
class FormContinent(ModelForm):
class Meta:
model = Continent
fields = '__all__'
class FormCountry(ModelForm):
class Meta:
model = Country
fields = '__all__'
views
def continent(request):
form = FormContinent()
if request.method == 'POST':
form = FormContinent(request.POST)
form.is_valid()
form.save()
return redirect('/continent')
else:
context = {'form': form}
return render(request, 'continent.html', context)
def country(request):
form = FormCountry()
if request.method == 'POST':
form = FormCountry(request.POST)
form.is_valid()
form.save()
return redirect('/country')
else:
context = {'form': form}
return render(request, 'country.html', context)
You can make use of the limit_choices_to=… parameter [Django-doc]:
class Country(models.Model):
continent = models.ForeignKey(
Continent,
# ↓ limit choices to Continents with status=True
limit_choices_to={'status': True},
null=True,
on_delete=models.SET_NULL
)
# …

Saving modelform with user id?

I have a question concerning a new project I'm creating. To put it simply, the website has user accounts, and each user has the ability to create a simple paragraph. The form is functioning perfectly, but I can't seem to assign the user's ID to the saved form.
model.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
class Thoughts(models.Model):
user = models.ForeignKey(UserProfile, null=True)
title = models.CharField(max_length=150, default='')
description = models.CharField(max_length=5000, default='')
forms.py
class ThoughtForm(ModelForm):
class Meta:
model = Thoughts
fields = ['title', 'description']
views.py
#login_required(login_url='sign_in')
def add_thought(request):
context = {}
populateContext(request, context)
user_details = UserProfile.objects.get(user=request.user)
context.update(user_details=user_details)
if request.method == 'POST':
new_thought_form = ThoughtForm(request.POST)
if new_thought_form.is_valid():
new_thought_form.save()
return HttpResponse('Hurray, saved!')
else:
new_thought_form = ThoughtForm()
c = {'new_thought_form': new_thought_form,}
c.update(csrf(request))
return render_to_response('lala/add_new_thought.html', c)
Whenever I try adding "instance=user_details.id", it says that the 'int' object has no attribute '_meta'. Any thoughts?
You can simplify the models by removing the UserProfile model:
# models.py
class Thoughts(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150, default='')
description = models.CharField(max_length=5000, default='')
Your forms.py looks good.
Change your views:
# views.py
#login_required(login_url='sign_in')
def add_thought(request):
if request.method == 'POST':
form = ThoughtForm(request.POST)
if form.is_valid():
thought = form.save(commit=False)
thought.user = request.user
thought.save()
return HttpResponse('Hurray, saved!')
else:
form = ThoughtForm()
return render(request, 'lala/add_new_thought.html', {
'form': form
})

django: ForeignKeyField limit_choices_to "parents pk"?

I've a model (Parent model):
class Post(models.Model):
image = models.ImageField(upload_to='%Y/%m/%d')
title = models.CharField(max_length=200)
width = models.DecimalField(max_digits=3, decimal_places=0)
height = models.DecimalField(max_digits=3, decimal_places=0)
year = models.PositiveIntegerField()
def __str__(self):
return self.title
and another model (Child model):
class Addimg(models.Model):
post = models.ForeignKey('Post', null=True)
addimg = models.ImageField(upload_to='%Y/%m/%d')
def __str__(self):
return self.post
My Addimg Form:
class AddimgForm(forms.ModelForm):
class Meta:
model = Addimg
fields = ('post', 'addimg', 'width', 'height',)
views.py using the form:
def addimg(request, pk):
if request.method == "POST":
form = AddimgForm(request.POST, request.FILES)
post = get_object_or_404(Post, pk=pk)
if form.is_valid():
addimg = form.save(commit=False)
addimg.addimg = request.FILES['addimg']
addimg.save()
return redirect('blog.views.detail', pk=post.pk)
else:
form = AddimgForm()
return render(request, 'blog/edit.html', {'form': form})
And my Problem is that when I create a "Child model" my post field returns all instances of allready created Post models as choices. What I want is that it automatic only displays the one Post it is related to without choices. Is ForeignKey the right model for that?
Any Ideas how this could work. thanks
ForeignKey field is translated into ModelChoiceField inside a Django ModelForm. If you inspect that class you will notice that this type of field has an queryset attribute required. By default Django provides the full set of objects. You can override this inside your form __init__ method by providing the parent object the form will need.
Consider the following example code:
def addimg(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = AddimgForm(request.POST, request.FILES, post=post)
#...
else:
form = AddimgForm(post=post)
return render(request, 'blog/edit.html', {'form': form})
class AddimgForm(forms.ModelForm):
class Meta:
model = Addimg
fields = ('post', 'addimg', 'width', 'height',)
def __init__(self, *args, **kwargs):
post = kwargs.pop('post')
super(AddimgForm, self ).__init__(*args, **kwargs)
self.fields['post'].queryset = Post.objects.filter(id=post.id)
What you want to do is create a Many-to-one relationship. For example,
post = models.ForeignKey('Post', null=True)
This means you can filter on it for example,
Addimg.objects.filter(post=Post)
or
Post.objects.get(pk=1)
Post.addimg_set.filter(xyz=etc)
Read more here: https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_one/