views.py
form = StudentTaskForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.instance.user = User.objects.get(id=request.user.id)
obj = form.save(commit=False)
obj.student = request.user
obj.todo = qs
obj.level = instance
obj.save()
ImageFormSet = modelformset_factory(Images,
form=ImageForm,min_num=0,
max_num=3, validate_min=True,extra=3)
if request.method == 'POST':
formset = ImageFormSet(request.POST, request.FILES,
queryset=Images.objects.none())
if formset.is_valid():
for form in formset.cleaned_data:
try:
image = form['image']
Images.objects.create(post=todo[0],image=image)
except KeyError:
pass
return redirect('student:dashboard')
else:
formset = ImageFormSet(queryset=Images.objects.none())
forms.py:
class StudentTaskForm(forms.ModelForm):
title = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-
control',' type': "text",'placeholder':'Enter Title'}))
content = forms.CharField(widget=SummernoteWidget())
class Meta:
model = Task
fields = [
'title',
'content',
]
widgets = {
'content': SummernoteWidget(),
}
def clean_object(self):
instance = self.instance
class ImageForm(forms.ModelForm):
image = forms.ImageField(label='Image')
class Meta:
model = Images
fields = ('image', )
Im using formsets for saving images.A task may contain a maximum of three images.
In models.py i have :
class Task(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
todo = models.ForeignKey(ToDo,
on_delete=models.CASCADE,related_name='todo')
student = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
content = models.TextField()
timestamp = models.TimeField(auto_now=True)
datestamp = models.DateField( auto_now=True)
like =
models.ManyToManyField(User,related_name='user_likes',blank=True)
is_verified=models.BooleanField(default=False,blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('student:dashboard')
objects = PostManager()
#property
def comments(self):
instance = self
qs = Comment.objects.filter_by_instance(instance)
return qs
#property
def get_content_type(self):
instance = self
content_type =
ContentType.objects.get_for_model(instance.__class__)
return content_type
class Images(models.Model):
post = models.ForeignKey(Task,
default=None,on_delete=models.CASCADE)
image = models.ImageField(verbose_name='Image',blank=True)
def __str__(self):
return self.post.title
Im creating another model to save the images that belong to a particular task
How do i target the post when submitting the form.?
Images.objects.create(post=todo[0],image=image)
Im confused what to put in post=? .By the definition of the model the post is a foreign key to a task object .i want this task object to be the model for currently submitting form model from the StudentTaskForm
You should be using an inline formset, which will handle this for you.
ImageFormSet = inlineformset_factory(Task, Images,
form=ImageForm, min_num=0,
max_num=3, validate_min=True, extra=3)
formset = ImageFormSet(request.POST, request.FILES, instance=obj)
if formset.is_valid():
formset.save()
Here there's no need to iterate at all.
Related
I am currently trying to create a form where users get to fill in their details after creating an account. The idea is that every user, after registration, gets redirected to this form page to fill it out. To achieve this, i'm using foreign keys.However it doesn't save to database.
models.py
class User(AbstractUser):
pass
def __str__(self):
return self.username
class Detail(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, default="")
first_name = models.CharField(max_length=200, default="")
last_name = models.CharField(max_length=255, default="")
class Meta:
verbose_name_plural = "Detail"
def __str__(self):
return self.first_name+ " "+self.last_name
forms.py
class Details(forms.ModelForm):
class Meta:
model = Detail
fields = "__all__"
widgets={
"user": forms.TextInput()
}
views.py
def details(request):
if request.method =="POST":
form = Details(request.POST)
if form.is_valid():
detail = form.save(commit=False)
detail.user = request.user
detail.first_name = detail.first_name.lower()
detail.last_name = detail.last_name.lower()
detail.save()
return redirect("admin:index")
else:
form = Details(initial={"user":request.user.username})
return render(request, "details.html", {"form":form})
You need to exclue user field from ModelForm like this
form.py
class Details(forms.ModelForm):
class Meta:
model = Detail
fields = "__all__"
exclude =["user"]
views.py
def details(request):
if request.method =="POST":
form = Details(request.POST)
if form.is_valid():
detail = form.save(commit=False)
detail.user = request.user
detail.first_name = detail.first_name.lower()
detail.last_name = detail.last_name.lower()
detail.save()
return redirect("admin:index")
else:
form = Details()
return render(request, "details.html", {"form":form})
I tried to make a creation form for vacancy on my job search site, but i faced with problem. I have User model, company model and vacancy model. They are inherited by foreignkeys. And the problem is that user can use all companies for creation a vacancy instead of created by this user companies(User can create several companies). I tried to change creation form and view by filtering, but it didn't work out for me. I am new at django and i dint find anything to resolve my problem.
Model of company:
class Company(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(('Title of Shop'), blank=True, max_length=255)
info = models.TextField(('Information about Shop'), null=True, blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.title)
Model of vacancy:
class Vacancies(models.Model):
title = models.CharField(('Title of Vacancy'), blank=True, max_length=255)
city = models.ForeignKey(City, on_delete=models.CASCADE, default='363')
description = models.TextField(('Information about Vacancy'), null=True, blank=True)
employer = models.ForeignKey(Company, on_delete=models.CASCADE)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-updated', '-created']
def __str__(self):
return str(self.title)
Create vacancy view:
#login_required(login_url='login')
def createVacancy(request):
form = VacanciesForm()
cities = City.objects.all()
if request.method == 'POST':
form = VacanciesForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
context = {'form': form, 'cities': cities}
return render(request, 'vacancy_form.html', context)
Vacancy form:
class VacanciesForm(ModelForm):
class Meta:
model = Vacancies
fields = '__all__'
What do I need to change to get the correct display of companies in the vacancy
Try this:
#login_required(login_url='login')
def create_vacancy(request):
user = request.user
cities = City.objects.all()
if request.method == 'POST':
form = VacanciesForm(data=request.POST, instance=user)
if form.is_valid():
form.save()
return redirect('home')
context = {'form': form, 'cities': cities}
return render(request, 'vacancy_form.html', context)
Also you can use in view:
def create_vacancy(request, logined_user_id):
vacancies = Vacancies.objects.filter(owner=logined_user_id)
...
And you can try some logic here:
class VacanciesForm(ModelForm):
vacancies = [some_logic]
class Meta:
model = Vacancies
fields = '__all__'
Finally, you can read docs:
modelforms
This work:
views
def createVacancy(request):
cities = City.objects.all()
form = VacanciesForm(data=request.POST, request=request)
if form.is_valid():
form.save()
return redirect('home')
context = {'form': form, 'cities': cities,}
return render(request, 'vacancy_form.html', context)
forms
class VacanciesForm(ModelForm, forms.Form):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super(VacanciesForm, self).__init__(*args, **kwargs)
self.fields['employer'].queryset = EmployerCard.objects.filter(owner=self.request.user)
class Meta:
model = Vacancies
fields = '__all__'
So I'm using the Django Form Wizard to split a simplified PostForm. In one of the steps, visitors can upload different images related to the Post.
Within the done method for the SessionWizardView, I'm saving the instance first and then check for the images within the formset.
However I get the following error message;
save() prohibited to prevent data loss due to unsaved related object
I tried setting the related Post id for the formset but I'm missing something here, formsets are still something I can't really follow.. Any help is appreciated!
models.py
class Post(models.Model)
title = models.CharField(max_length=200)
description = models.TextField(max_length=1000)
def __str__(self):
return self.title
class Image(models.Model):
post = models.ForeignKey('Post', on_delete=models.SET_NULL, null=True)
file = models.ImageField(upload_to='images/', null=True, blank=True)
alt = models.CharField(max_length=200, blank=True)
views.py
FORMS = [
('title', PostCreateForm),
('image', ImageFormset)
]
TEMPLATES = {
'title': 'post_form_title.html',
'image': 'post_form_image.html'
}
class PostWizardView(SessionWizardView):
form_list = FORMS
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'temp/'))
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list, form_dict, **kwargs):
instance = Post()
for form in form_list:
instance = construct_instance(form, instance)
instance.save()
post_id = instance.pk
if 'image' in form_dict:
formset = form_dict['image']
if formset.is_valid():
formset.save(commit=False)
for form in formset:
form.post = post_id
formset.save()
forms.py
class PostCreateForm(forms.ModelForm):
class Meta:
model = Image
fields = '__all__'
ImageFormset = inlineformset_factory(
Post,
Image,
form = PostCreateForm,
min_num=1,
can_delete=True
)
You should save the individual forms as you loop through them:
if formset.is_valid():
for form in formset:
if form.is_valid():
f = form.save(commit=False)
f.post = post_id
f.save()
I need to display just the selected value from a forms.ModelChoiceField on a view page. How would I do that?
I've looked at many different forums and couldn't get a clear answer on what I should do in my case. I am new at Python.
form:
class Manufacturer1Form(ReadOnlyFormMixin, ModelForm):
manufacturer = forms.ModelChoiceField(queryset=Vendor.objects.filter(vendor_type='manufacturer').order_by('name'))
class Meta:
model = Manufacturer1Relationship
exclude = ('part',)
model:
class Manufacturer1Relationship(models.Model):
part = models.ForeignKey(Part, on_delete=models.CASCADE)
manufacturer = models.ForeignKey(Vendor, on_delete=models.CASCADE,
limit_choices_to={'vendor_type': 'manufacturer'},)
partNumber = models.CharField(max_length=40, blank=True)
class Meta:
permissions = (
('modify_admin_site', 'Can modify, add, view, or delete manufacturer relationships'),
)
view:
def PartView(request, type_id, id):
partType = Type.objects.get(id=type_id)
instance = get_object_or_404(Part, id=id)
selection = None
if request.method == 'POST':
form = ViewPartForm(type_id, request.POST, request.FILES, instance=instance)
manu1_formset = ManufacturerFormSet(request.POST, instance=instance)
location1_formset = LocationFormSet(request.POST, instance=instance)
if form.is_valid():
selection = form.cleaned_data['active_feed']
part = form.save(commit=False)
part.partType_id = type_id
if manu1_formset.is_valid() and location1_formset.is_valid():
part.save()
manu1_formset.save()
location1_formset.save()
url = reverse('list_parts', args=[partType.pk])
return HttpResponseRedirect(url)
else:
form = ViewPartForm(type_id=type_id, instance=instance)
manu1_formset = ManufacturerFormSet(instance=instance)
location1_formset = LocationFormSet(instance=instance)
return render(request, 'part_view.html', {'view_part_form': form,
'location_formset': location1_formset,
'manu_formset': manu1_formset,
'selection': selection,
'partType': partType,
'part': instance})
class Manufacturer1Form(ReadOnlyFormMixin, ModelForm):
manufacturer = forms.ModelChoiceField(queryset=Vendor.objects.filter(vendor_type='manufacturer').order_by('name'))
class Meta:
model = Manufacturer1Relationship
exclude = ('part',)
def __init__(self, *args, **kwargs):
initial_manufacturer = kwargs.pop("manufacturer",None)
super().__init__(*args, **kwargs)
self.fields["manufacturer"].initial = initial_manufacturer
ManufacturerFormSet(request.POST, instance=instance, manufacturer=specific_manufacturer)
i create simple page where i can add article (title, text and category). When i do it on 'site administration everything is ok', but when i add it on page i can't choose category.
http://i.stack.imgur.com/4Nxzb.jpg
I choose category, for example like on this screen but my article do not have this category after i save it (article is uncategorized).
I created forms.py file and i done it like this:
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'text', 'categories')
How must i change 'categories' here to make it usable?
models.py:
from django.db import models
from django.utils import timezone
class Category(models.Model):
name = models.CharField('Nazwa Kategorii', max_length=100)
slug = models.SlugField('Odnośnik', max_length=100)
icon = models.ImageField('Ikonka Kategorii', upload_to='icons',
blank=True)
class Meta:
verbose_name = "Kategoria"
verbose_name_plural = "Kategorie"
def __unicode__(self):
return self.name
class Post(models.Model):
author = models.CharField(max_length=25, blank=True, null=True)
title = models.CharField(max_length=200)
slug = models.SlugField('Odnośnik', max_length=255)
text = models.TextField()
published_date = models.DateTimeField(
default=timezone.now)
categories = models.ManyToManyField(Category, verbose_name='Kategorie')
def publish(self):
self.published_date = timezone.now()
self.save()
def __unicode__(self):
return self.title
And from views.py my view
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('blog.views.post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
When you save a form using commit=False you should call the save_m2m() method of the form:
post = form.save(commit=False)
post.author = request.user
post.save()
form.save_m2m()
Another, more elegant, solution is to pass the instance with preset field to the form's constructor. Is this case you don't need to use the commit=False argument:
form = PostForm(request.POST, instance=Post(author=request.user))
if form.is_valid():
post = form.save()
return redirect('blog.views.post_detail', pk=post.pk)