Django_tables2: Dynamically hiding columns based on the request - python

I have a table that's based on a model that has several fields. I also have two TemplateColumns, one for editing the specific entity and another for deleting it. Here is my code:
class EntitetTable(tables.Table):
edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
orderable=False, visible=False)
delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
orderable=False, visible=False)
class Meta:
abstract = True
attrs = {'class': 'paleblue', }
class TipPredmetaTable(EntitetTable):
class Meta(EntitetTable.Meta):
model = models.TipPredmeta
Now, I have a user hierarchy in my system and only users that are bookkeepers can edit and delete the data. That being said, I tried to implement a check in my view to hide the two TemplateColumns:
#login_required
def tippredmeta(request):
try:
korisnik = request.user.radnik
except ObjectDoesNotExist:
return HttpResponseRedirect("/main/")
queryset = TipPredmeta.objects.all()
table = TipPredmetaTable(queryset)
if korisnik.is_kustos:
table.edit.visible = True
table.delete.visible = True
RequestConfig(request).configure(table)
return render_to_response('azuriranje/tabelaPrikaz.html', {'table': table, },
context_instance=RequestContext(request))
However, I get the following exception on the table.edit.visible = True line:
Exception Type: AttributeError
Exception Value: 'TipPredmetaTable' object has no attribute 'edit'
Now, here are the things I've tried:
- First I thought about using fields and exclude, but I couldn't alter that dynamically.
- Then I thought about placing all of this into the __init__ method, effectively making edit and delete attributes of my EntitetTabel (the idea was to solve the error) but while the error was gone, so were my TemplateColumns. I tried displaying them via fields, but that didn't help. My guess is that the superclass, the tables.Table, doesn't work like that.

You can use the exclude attribute of the tables.Table for excluding unwanted fields. It also works after creating the table instance. So in your case, you can do something like this.
First leave your columns visible (i removed the visible=False):
class EntitetTable(tables.Table):
edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
orderable=False)
delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
orderable=False)
Then based on your condition, you can exclude the edit and delete fields:
table = TipPredmetaTable(queryset)
if not korisnik.is_kustos:
table.exclude = ('edit', 'delete',)

Related

Django - Problem adding a queryset to a field on a FilterSet

I have some filtersSets working fine, but now I tried to add a queryset to a field on the FilterSet and it fails when I load the page.
I'm using Django 2.1.1 with Python 3.6 and Django-filter 2.0.0.
view
def search_jobs(request):
job_list = Job.objects.filter(project__status="P", is_deleted="False")
job_filter = JobsListFilter(request.GET, queryset=job_list)
return render(request, 'webplatform/jobs_list_filter.html', {'filter': job_filter})
filter
class JobsListFilter(django_filters.FilterSet):
# LINE ADDED - the following line is the added queryset
projects = Project.objects.filter(status="P", is_deleted="False")
skills = WorkerSkills.objects.filter(
id__in=Job.objects.values_list('required_skills__id', flat=True).distinct())
worker_job = django_filters.CharFilter(method='my_filter')
required_skills = django_filters.ModelMultipleChoiceFilter(queryset=skills, widget=forms.SelectMultiple)
# LINE ADDED - The following line is the one that adds the queryset inside the field I want to filter.
project = django_filters.ChoiceFilter(queryset=projects)
compensation_type = django_filters.ChoiceFilter(choices=Job.COMPENSATION_TYPE, widget=forms.RadioSelect)
class Meta:
model = Job
fields = ['worker_job', 'required_skills', 'project', 'compensation_type']
def my_filter(self, queryset, worker_job, value):
return queryset.filter(
worker_job__icontains=value
) | queryset.filter(
work_description__icontains=value
)
The code is working without the added lines LINE ADDED on the FilterSet. But the thing is that on the field project it just let me select between all the projects created, and I want to have only the ones that are really necessary (applying the queriset on the code).
But adding those lines in the code, when I use debug mode I can see the queryset applied on the field project giving the expected results. But then, on the return of the view is throwing the following error.
TypeError at /platform/search/jobs/
__init__() got an unexpected keyword argument 'queryset'
So I don't know what I did wrong, because I'm using the same structure used on the required_skills field adding a queryset with only the objects that I want and it should work.
As #Moisés Hiraldo said on the comments, the problem is that I had to use django_filters.ModelChoiceFilter in front of django_filters.ChoiceFilter.

Check if record exists in Django Rest Framework API LIST/DATABASE

I want to create a viewset/apiview with a path like this: list/<slug:entry>/ that once I provide the entry it will check if that entry exists in the database.
*Note: on list/ I have a path to a ViewSet. I wonder if I could change the id with the specific field that I want to check, so I could see if the entry exists or not, but I want to keep the id as it is, so
I tried:
class CheckCouponAPIView(APIView):
def get(self, request, format=None):
try:
Coupon.objects.get(coupon=self.kwargs.get('coupon'))
except Coupon.DoesNotExist:
return Response(data={'message': False})
else:
return Response(data={'message': True})
But I got an error: get() got an unexpected keyword argument 'coupon'.
Here's the path: path('check/<slug:coupon>/', CheckCouponAPIView.as_view()),
Is there any good practice that I could apply in my situation?
What about trying something like this,
class CheckCouponAPIView(viewsets.ModelViewSet):
# other fields
lookup_field = 'slug'
From the official DRF Doc,
lookup_field - The model field that should be used to for performing
object lookup of individual model instances. Defaults to pk

Confuse for using get queryset and using pass to ignore an exception and proceed in my django view

I wrote a view to update my draft object , before updating my draft I need to see if any draft exists for package(draft.package) in db or not .
If any draft available, i need to update that draft's fields.
I am using get queryset to look into db to check draft availability.
I want to know that using get queryset here is good way or not and using pass into except.
My View
def save_draft(draft, document_list):
"""
"""
try:
draft = Draft.objects.get(package=draft.package)
except Draft.DoesNotExist as exc:
pass
except Draft.MultipleObjectsReturned as exc:
raise CustomException
else:
draft.draft_document_list.filter().delete()
draft.draft_document_list.add(*document_list)
draft.save()
Extra Information :
models.py
class Package(models.Model):
name = models.CharField(max_length=100)
# -- fields
class Document(models.Model):
# -- fields
Class Draft(models.Model):
# --- fields
package = models.ForeignKey(Package)
draft_document_list = models.ManyToManyField(Document)
My Algorithm :
# first check to see if draft exists for package
# if exists
# overwrite draft_document_list with existed draft and save
# if none exists
# update passed draft object with draft_document_list
Input variables
save_draft(draft, document_list)
draft --> latest draft object
document_list --> list of documents mapped with Draft as M2M.
Yes, for for you models and method signature you use get right. To simplify things you can get rid of delete()/add() methods by direct assign document_list to M2M relation.
def save_draft(draft, document_list):
try:
draft = Draft.objects.get(package=draft.package)
except Draft.DoesNotExist:
pass
except Draft.MultipleObjectsReturned:
raise CustomException
draft.draft_document_list = document_list
draft.save()
EDIT: If there can be only one draft per package then why you use ForeignKey(Package)? With OneToOne relation your code will be much simpler:
def save_draft(draft, document_list):
draft.draft_document_list = document_list
draft.save()

Django Admin Inlines with many fields, possible to have inline add button create popup?

I've got 3 models:
class Top(models.Model):
toptitle = models.CharField(max_length=255, verbose_name='top title')
class Middle(models.Model):
top = models.ForeignKey(Top)
middletitle = models.CharField(max_length=255, verbose_name='middle title')
importantfield1 = models.TextField(verbose_name='important field 1')
importantfield2 = models.TextField(verbose_name='important field 2')
...
importantfield20 = models.TextField(verbose_name='important field 20')
Now when I'm viewing the Admin page for a Top, I want to see what Middles are related to it (and be able to add and edit Middles from this page or from a link on this page).
I can do that easily enough with inlines. The problem is it becomes unwieldy with so many (required) fields in Middle.
If I specify that the inline is only to show middletitle
class MiddleInline(admin.TabularInline):
model = MiddleInline
fields = ('middletitle',)
extra = 0
I've got two problems. The first is that there is no way for me to get to the page on which I can edit all of the fields for Middles that already exist (without having to go to the Admin menu, selecting Middle and having to find the right Middle there). The second problem is that if I try to add another Middle from this inline, it allows me to create a middle with just a middletitle, but leaving empty all of the required importantfields.
I've been able to deal with the first issue by adding a link to edit the object:
class MiddleInline(admin.TabularInline):
def changeform_link(self,instance):
if instance.id:
changeform_url = reverse(
'admin:myapp_middle_change', args=(instance.id,)
)
return 'Details'
return ''
changeform_link.allow_tags = True
changeform_link.short_description = '' # omit column header
model = MiddleInline
fields = ('middletitle','changeform_link')
extra = 0
but now I'm not sure how to deal with the second problem.
Ideally I'd like the 'Add Another Middle' section to open up a pop up for creating a new Middle (with the Top already set and having/requiring all of the importantfields), which when saved would refresh the inline.
Is there a way of doing this? Am I approaching this entirely wrong?
I'm pretty sure I had somewhat the same issue. My solution was to display two different fieldsets for two different situations.
The ModelAdmin class has a get_fieldsets(self,request, obj=None) functions which i override in my admin.py file.
so something like:
class MiddleInline(admin.TabularInline):
'''your stuff'''
def get_fieldsets(self, request, obj=None):
if obj is None:
fields = list(('''tuple of the fields you want to display if it's a new object'''))
else:
fields = list(('''tuple of the fields you want to display if it's not a new object'''))
return [(None, {'fields': fields})]
I am not quite entirely sure I got your question right, but I hope this can help!

How to validate django form only when adding not editing

How could we make the django form to not validate if we are editing, not adding a new record. The code as following :
class PageForm(forms.Form):
name = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class':'textInput'}))
description = forms.CharField(max_length=300, required=False,widget=forms.TextInput(attrs={'class':'textInput'}))
body = forms.CharField(widget=forms.Textarea)
template = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'class':'textInput'}))
navbar = forms.BooleanField(required=False, widget=forms.Select(choices=(('True','True'),
('False', 'False'))))
publish = forms.BooleanField(widget=forms.Select(choices=(('Published','Publish Now'),
('Private','Private'),
('Draft','Draft'))))
def save(self, page=None, commit=True):
data = self.cleaned_data
if not page:
page = models.Page(key_name=data['name'].replace(' ','-'))
page.name = data['name']
page.description = data['description']
page.body = data['body']
page.template = data['template']
page.publish = data['publish']
if commit: page.put()
return page
# prevent the same page 's name
def clean_name(self):
name = self.cleaned_data['name']
query = models.Page.all(keys_only=True)
query.filter('name = ', name)
page = query.get()
if page:
raise forms.ValidationError('Page name "%s" was already used before' % name)
return name
The purpose of this name validation is to prevent the records with the same name. BUt i found that, it also validate on edit, so we couldn't edit records, since it will said 'records with same name already exist'.
Actually for editing, the page param on save function wont be none, but prev record instead, and wil be none on saving a new one. But how we read this param, on clean_name function so we can now whether it is editing or creating?
Thanks a lot!
in your clean method, you can use self.initial to know whether it is adding or editing. If it is editing, the self.initial will not be empty. But when it is adding, self.initial will be dictionary of what the previous value.
If you are editing form, then the form has some instance, and you can check if that exists.
If it does, then you are probably editing existing object.. right?
Example:
If you are editing object with form, you create form object much like this:
form = MyForm(instance = myobject)
Then in your form class methods you can check if form has saved instance in a way that it is described here:
Test if Django ModelForm has instance
in your clean_name function exclude the current object from queryset
query.filter('name = ', name).exclude(pk=self.pk)
or change the if condition to check that page and current object are not the same.
Sorry, I couldn't comment below your guys post, don't know why.
#sunn0 : I didn't use django models, coz deploy the app in appengine, so use appengine model instead.
#Zayatzz : May you show a little code how to do it? Since whether we are adding or editing, we always bound the form to request.POST before validation, so don't know how to differentiate.
#Ashok : I made a workaround based on your suggestion. Since previously I didn't pass the pk to form, but passing the prev object as param instead, so couldn't exclude by using pk. So, I change the code and put additional key as pk (if create, let key empty, but if edit fill key with pk) and just check in if condition, if key field not empty, then it means we are editing. Not sure if it is best practice, but it works anyway.
I can suggest to override form's init method
https://stackoverflow.com/a/70845558/15080117
because there is an argument instance.

Categories

Resources