I have two questions in regards to the problem I am currently facing:
Is it best practice in django to overwrite the post method in the CreateView? If it isn't do you write a form _valid function on the CategoryFullForm or in the CreateView and how would it look? The CreateView currently works great, but want to make sure there isn't a better way to do this.
If this is best practice, how would you override the get function in the UpdateView so you would be able to edit the files that relate to the instance being uploaded and even add an additional File? Open to other ways to accomplish this task as well.
models.py
############ CATEGORY MODEL ################
class Category(models.Model):
category_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, null=True, blank=True)
est_pr_sqft = models.FloatField(blank=True)
est_duration = models.IntegerField(blank=True)
preceding_job = models.CharField(max_length=100, blank=True)
category_notes = models.CharField(max_length=250, blank=True)
category_slug = models.SlugField(max_length=100, unique=True, null=False)
author = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
date_posted = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('category-detail', kwargs={'slug': self.category_slug})
def save(self, *args, **kwargs):
if not self.category_slug:
self.category_slug = slugify(self.name)
return super().save(*args, **kwargs)
class CategoryFiles(models.Model):
category_id = models.ForeignKey(Category, on_delete=models.CASCADE)
document = models.FileField(upload_to="attachments",null=True,blank=True)
uploaded_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def delete(self, *args, **kwargs):
self.document.delete()
return super().delete(*args, **kwargs)
forms.py
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ['name', 'est_pr_sqft', 'est_duration', 'preceding_job', 'category_notes']
class CategoryFullForm(CategoryForm):
files = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}),required=False)
class Meta(CategoryForm.Meta):
fields = CategoryForm.Meta.fields + ['files']
views.py
####### CATEGORY VIEWS ########
class CategoryListView(LoginRequiredMixin, ListView):
model = Category
template_name = 'ProjectManagementApp/PM-Category-List.html'
context_object_name = 'categories'
slug_field = 'project_slug'
success_url = reverse_lazy('Category-list')
class CategoryDetailView(LoginRequiredMixin, DetailView):
model = Category
template_name = 'ProjectManagementApp/PM-Category-Detail.html'
slug_field = 'category_slug'
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
context['files'] = CategoryFiles.objects.all()
#context['photos'] = CategoryPhotos.objects.all()
return context
class CategoryCreateView(LoginRequiredMixin, CreateView):
model = Category
form_class = CategoryFullForm
template_name = 'ProjectManagementApp/PM-Category-Create.html' # Replace with your template.
#success_url = reverse_lazy('category-detail')
slug_field = 'category_slug'
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('files')
if form.is_valid():
author = request.user
name = form.cleaned_data['name']
est_pr_sqft = form.cleaned_data['est_pr_sqft']
est_duration = form.cleaned_data['est_duration']
preceding_job = form.cleaned_data['preceding_job']
category_notes = form.cleaned_data['category_notes']
category_obj = Category.objects.create(name=name,est_pr_sqft=est_pr_sqft,est_duration=est_duration,preceding_job=preceding_job,category_notes=category_notes,author=author)
for f in files:
CategoryFiles.objects.create(category_id=category_obj,document=f)
return HttpResponseRedirect(reverse_lazy('Category-list'))
else:
return self.form_invalid(form)
class CategoryUpdateView(LoginRequiredMixin, UpdateView): #UserPassesTestMixin - makes sure user who made entry is only one who can update it.
model = Category
form_class = CategoryFullForm
template_name = 'ProjectManagementApp/PM-Category-Create.html' # Replace with your template.
slug_field = 'category_slug'
success_url = reverse_lazy('Category-list')
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('files')
if form.is_valid():
author = request.user
name = form.cleaned_data['name']
est_pr_sqft = form.cleaned_data['est_pr_sqft']
est_duration = form.cleaned_data['est_duration']
preceding_job = form.cleaned_data['preceding_job']
category_notes = form.cleaned_data['category_notes']
category_obj = Category.objects.create(name=name,est_pr_sqft=est_pr_sqft,est_duration=est_duration,preceding_job=preceding_job,category_notes=category_notes,author=author)
for f in files:
CategoryFiles.objects.create(category_id=category_obj,document=f)
return HttpResponseRedirect(reverse_lazy('Category-list'))
else:
return self.form_invalid(form)
#
# #Doesnt allow you to update other users post
# #def test_func(self):
# # project = self.get_object()
# # if self.request.user == post.author:
# # return True
# # return False
#
#
def form_valid(self,form):
form.instance.author = self.request.user
return super().form_valid(form)
class CategoryDeleteView(LoginRequiredMixin, DeleteView): #UserPassesTestMixin - makes sure user who made entry is only one who can update it.
model = Category
template_name = 'ProjectManagementApp/PM-Category-Delete.html'
slug_field = 'category_slug'
success_url = reverse_lazy('Category-list')
#Doesnt allow you to delete other users post
#def test_func(self):
# project = self.get_object()
# if self.request.user == post.author:
# return True
# return False
If the urls.py/admin.py are needed am happy to provide, but don't think they are needed, as well as any of the html files.
Related
I want to change Photo 'labeled' field False when I delete LabeledPhoto object.
#models.py
class Photo(models.Model):
image = models.ImageField(upload_to='shoes_data/%Y/%m/%d', name='image')
created = models.DateTimeField(auto_now_add=True)
labeled = models.BooleanField(default=False)
class LabeledPhoto(models.Model):
labeled_image = models.OneToOneField(Photo, on_delete=models.PROTECT, related_name='labeled_image')
topcategory = models.CharField(max_length=64)
subcategory = models.CharField(max_length=64)
labeler = models.CharField(max_length=32)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
I tried like this but, It didn't work
# views.py
class LabeledPhotoDelete(DeleteView):
model = LabeledPhoto
template_name = 'label/labeled_photo_delete.html'
success_url = reverse_lazy('photo:labeled_list')
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
labeled = LabeledPhoto.objects.get(id=self.object.pk)
labeled.labeled_image.labeled = False
labeled.save()
self.object.delete()
return reverse(success_url)
You need to .save() the Photo:
from django.http import HttpResponseRedirect
class LabeledPhotoDelete(DeleteView):
model = LabeledPhoto
template_name = 'label/labeled_photo_delete.html'
success_url = reverse_lazy('photo:labeled_list')
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
image = self.object.labeled_image
image.labeled = False
image.save()
self.object.delete()
return HttpResponseRedirect(success_url)
But there is no reason to store whether the image is labeled in a field: you can simply look that up if you want to know this. Indeed, you can add a property:
class Photo(models.Model):
image = models.ImageField(upload_to='shoes_data/%Y/%m/%d', name='image')
created = models.DateTimeField(auto_now_add=True)
#property
def labeled(self):
try:
return self.labeled_image is not None
except LabeledPhoto.DoesNotExist:
return False
or annotate the queryset with the fact if a LabeledPhoto exists:
from django.db.models import BooleanField, ExpressionWrapper, Q
Photo.objects.annotate(
is_labeled=ExpressionWrapper(
Q(labeled_image__isnull=False),
output_field=BooleanField()
)
)
I've read previous Q and docs, however still not able to make it work..
I have the following models:
class Process(models.Model):
class Meta:
verbose_name_plural = "Processes"
name = models.CharField(max_length=120)
description = models.CharField(max_length=4)
parent_process = models.ForeignKey('self', related_name="parent_process+", on_delete=models.PROTECT, blank=True, null=True, verbose_name="parent process")
process_activities = models.CharField(max_length = 2048, blank=True, verbose_name="process activites")
owner = models.ForeignKey(User, related_name="owner+", on_delete=models.PROTECT, blank=True, null=True, verbose_name="owner")
def __str__(self):
return "{}".format(self.name)
class ControlTest(models.Model):
history = AuditlogHistoryField()
name = models.CharField(max_length=120)
description = models.CharField(max_length=120)
Now I want to use an accessor to access the name of the process and display in a table (tables.py):
class controltestTable(tables.Table):
class Meta:
hoofdproces = tables.Column(accessor='process.name')
model = ControlTest
fields = ['id',"name", "hoofdproces", "sub_process", 'description', 'control', 'delegate', 'owner', 'reviewer1', 'reviewer2', 'reviewer1_r', 'reviewer2_r', 'delegate_r',
'owner_r', 'reviewer_status', 'status', 'history', 'comments', 'review_comments', 'due_date', 'review1_due_date', 'review2_due_date', 'documentation']
template_name = "django_tables2/bootstrap-responsive.html"
Views.py:
class ControlTestView(SingleTableView, FormView, SingleTableMixin, FilterView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
queryset = kwargs.pop('object_list', None)
if queryset is None:
self.object_list = self.model.objects.all()
context['controltests'] = ControlTest.objects.all()
return context
def post(self, request, *args, **kwargs):
form = self.get_form()
print(form)
if form.is_valid():
instance = form.save(commit=True)
instance.save()
print('valid')
return self.form_valid(form)
else:
print(form.errors)
return self.form_invalid(form)
template_name = "control/ControlTest.html"
model = ControlTest
form_class = ControlTestForm
table_class = controltestTable
success_url = reverse_lazy('controltest')
However, nothing gets displayed in the hoofdproces column :(
How is that possible? What am I doing wrong?
Please help!
I would like to share with you my code in order to find a solution. I have a Django form. I would like to save data only if there is not another object with same data. In other words, objects should be unique.
If the object doesn't exist, I save it, else I display the form with an error message 'The object already exists with this features'.
This is my model:
def guide_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/guides/<guide_id>
new_filename = f'guides/{instance.site.id}'
if instance.profile_type:
new_filename += f'_{instance.profile_type}'
if instance.profile_level:
new_filename += f'_{instance.profile_level}'
if instance.language:
new_filename += f'_{instance.language}'
name, ext = os.path.splitext(filename)
if ext:
new_filename += ext
return new_filename
class UserGuide(models.Model):
""" A class for storing user guides depending on profiles """
objects = models.Manager()
site = models.ForeignKey(WebApplication, on_delete=models.PROTECT, related_name='guides',
verbose_name=_('application'))
file = models.FileField(verbose_name='file', upload_to=guide_path)
profile_type = models.CharField(verbose_name=_('profile type'), choices=UserProfile.USER_TYPES, max_length=2,
null=True, blank=True)
profile_level = models.CharField(verbose_name=_('profile level'), choices=UserProfile.USER_ROLES, max_length=2,
null=True, blank=True)
language = models.CharField(verbose_name=_('language'), choices=settings.LANGUAGES, max_length=2, default='en')
class Meta:
verbose_name = _('user guide')
verbose_name_plural = _('user guides')
unique_together = ('site', 'profile_type', 'profile_level', 'language')
This is my form:
class UserGuideForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class Meta:
model = UserGuide
fields = ['site', 'file', 'profile_type', 'profile_level', 'language']
widgets = {
'file': CustomFileInput(attrs={'class': 'clearablefileinput'}),
}
And this is my view:
class UserGuideUpdateView(UpdateView):
""" Display a form to create a userguide
**Context**
``subtitle``
Title of the page
**Template:**
:template:`app/generic_form.html`
"""
model = UserGuide
form_class = UserGuideForm
success_url = reverse_lazy('userguide-list')
template_name = 'app/form_userguide.html'
permission_required = 'app.change_userguide'
def get_object(self, queryset=None):
pk = self.kwargs.get('pk')
return get_object_or_404(UserGuide, pk=pk)
def get_title(self):
return _('Edit user guide: ')
def get_context_data(self, **kwargs):
context = super(UserGuideUpdateView, self).get_context_data(**kwargs)
context.update({
'subtitle': self.get_title(),
})
return context
def form_valid(self, form):
site = form.cleaned_data['site']
file = form.cleaned_data['file']
profile_type = form.cleaned_data['profile_type']
profile_level = form.cleaned_data['profile_level']
language = form.cleaned_data['language']
userguide = UserGuide.objects.filter(site=site.id, profile_type=profile_type, profile_level=profile_level, language=language)
if userguide.exists():
messages.error(self.request, _('A user guide for that profile and language already exists'))
HttpResponseRedirect(self.template_name)
else:
pass
return super().form_valid(form)
How I can add condition, if my object already exists, not save the form and return the form with the error message ?
Thank you
I have trouble getting the current instance's fields on my UpdateView. How do I get the specific instance based on its id?
views.py
class ShowUpdate(UpdateView):
model = Show
fields = ['description', 'season', 'episode']
def post(self, request, **kwargs):
request.POST = request.POST.copy()
request.POST['description'] = "how to get instance description?" # problem here
request.POST['season'] = 2
return super(ShowUpdate, self).post(request, **kwargs)
models.py
class Show(models.Model):
owner = models.ForeignKey(User, null=True, default=True, related_name='o')
title = models.CharField(max_length=100)
description = models.TextField(default='N/A', blank=True, max_length=250)
season = models.IntegerField(default=0)
episode = models.IntegerField(default=0)
def get_absolute_url(self):
return reverse('show:index')
def __str__(self):
return self.title
Look to the UpdateView docs
This View has method get_object(self, queryset=None)
In you case just need to call it in POST method something like this:
class ShowUpdate(UpdateView):
model = Show
fields = ['description', 'season', 'episode']
def post(self, request, **kwargs):
self.object = self.get_object()
request.POST = request.POST.copy()
request.POST['description'] = self.object.description
request.POST['season'] = 2
return super(ShowUpdate, self).post(request, **kwargs)
I am trying to allow a user to edit the information entered into their project using UpdateView. I kept getting an error saying "Generic detail view UpdateProject must be called with either an object pk or a slug." I have tried pointing to the slug within the UpdateView class, but can't get it to work. I am new to Django, so any help would be much appreciated!
views
class CreateProject(CreateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/create_project.html'
def form_valid(self, form):
userproject = form.save(commit=False)
form.instance.user = self.request.user
userproject.save()
self.object = userproject
return super(CreateProject, self).form_valid(form)
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
class UpdateProject(UpdateView):
model = UserProject
form_class = UserProjectForm
template_name = 'howdidu/update_project.html'
slug_field = 'slug'
def form_valid(self, form):
userproject = form.save(commit=False)
form.instance.user = self.request.user
userproject.save()
self.object = userproject
return super(UpdateProject, self).form_valid(form)
def get_success_url(self):
project_username = self.request.user.username
project_slug = self.object.slug
return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})
urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register_profile/$', views.register_profile, name='register_profile'),
url(r'^update_profile/$', views.update_profile, name='update_profile'),
url(r'^create_project/$', login_required(views.CreateProject.as_view()), name='create_project'),
url(r'^update_project/$', login_required(views.UpdateProject.as_view()), name='update_project'),
url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
url(r'^(?P<username>\w+)/(?P<slug>[-\w]+)/$', views.project_page, name='user_project'),
)
models
class UserProject(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
project_overview = models.CharField(max_length=1000)
project_picture = models.ImageField(upload_to='project_images', blank=True)
date_created = models.DateTimeField(auto_now_add=True)
project_views = models.IntegerField(default=0)
project_likes = models.IntegerField(default=0)
project_followers = models.IntegerField(default=0)
slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(UserProject, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
Django's UpdateView requires pk or slug parameter coming from the corresponding url by default. In your case you need to change the url to your UpdateProject view to
url(r'^update_project/(?P<slug>[-\w]+)/$',
login_required(views.UpdateProject.as_view()),
name='update_project'),
where slug is your project's slug.
create a utils.py file in your app. using slugify from django utils.
import random
import string
from django.utils.text import slugify
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return "".join(random.choice(chars) for _ in range(size))
def unique_slug_generator(instance, new_slug=None):
if new_slug is None:
slug = new_slug
else:
slug=slugify(instance.model field of your choice)
Klass = instance.__class__
qs_exists = Klass.objects.filter(slug=slug).exists()
if qs_exists:
new_slug = "{slug}-{randstr}".format(
slug=slug,
randstr=random_string_generator(size=4)
)
return unique_slug_generator(instance, new_slug=new_slug)
return slug
add a slug to your model
slug = models.SlugField(null=True, blank=True)
add it to your urls.py file
url(r'^update/(?P<slug>[\w-]+)/$', profileUpdate.as_view(), name='update'),
class UserProject(UpdateView):
model = BlogSlider
fields = ['title', 'sub_title', 'description', 'image']
template_name = 'update.html'
use a form and use {{ form.as_p }}.