How to change permission in Django view sub-class? - django

I have the following setup right now:
class MyView(MyPermission1, MyPermission2, FormView):
...
class MyChildView(MyView):
...
The child view is inheriting from the parent, but I want to remove MyPermission1 and MyPermission2 from it and apply another class called MyPermissionChild. Is there a way to do this using the generic views available in Django?

Related

Where does a Django generic view class call the database?

I can inherit from a class based generic view from generic.ListView as in
class BookListView(generic.ListView):
model = Book
Supposedly the .as_view() function gets called when the Django app starts up to create an instance of this view.
I find the definition of get_queryset in MultipleObjectMixin but what I dont find is, from where is this method called from?

Django Admin Site displays the list of 'XXXObject' rather than directly displaying the details of the Objects

These are the Apps added from the Model Class
I want it to directly display the details of the Object
The attributes of the Leave_Details class are start_date,end_date,details..
Please click on the image link.
How can I directly display the attributes of all the Leave_Object class in Home>App>Leave_statuss
Display it just like the inbuilt Users model i.e. in a systematic table
You need to implement
def __unicode__(self):
return self.[attribute_name]
to retrive the Object's name as attribute_name.
You should define Admin class derived from admin.ModelAdmin and define your fields like follows:
in admin.py of your app define
class LeaveDetailsAdmin(admin.ModelAdmin):
list_display = ['start_date', 'end_date']
admin.site.register(Leave_Details, LeaveDetailsAdmin)
RTFM :) - https://docs.djangoproject.com/en/1.10/ref/contrib/admin/
To be more specific: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#modeladmin-objects

How to use the same clean() method in two different forms in django?

I have two forms NewItem(ModelForm) and EditItem(ModelForm). I overrode the clean() method of NewItem(ModelForm) to validate the fields. I would like to re-use the same clean() method in EditItem(ModelForm) incase the use tries to edit and re-save the data? Is there a clean way to achieve this without copy and paste?
Yes, you can create a mixin class named FormCleanMixin() which will contain the clean() method common between the 2 forms. Then inherit this mixin class in your 2 forms.
First, create the mixin class like:
from django.forms import ModelForm
class FormCleanMixin(ModelForm):
def clean(self):
...
# your common code for 'clean()' here
Now, inherit this mixin class in your two forms like:
class NewItem(FormCleanMixin): # inherit the mixin
... # your code
class EditItem(FormCleanMixin): # inherit the mixin
... # your code
You could use inheritance for that:
make EditItem inherit from NewItem: EditItem(NewItem)
or both forms inherit from a class that defines only the clean() method, for example CleanItemForm(ModelForm), and then you defineNewItem(CleanItemForm) and EditItem(CleanItemForm).
Note: I'm new to Django and there may be another method that I don't know about.

Django - How to use a custom class in a view?

I've coded some classes in pure python, but now I need to use that classes in a django view.
my_custom_classes.py
class RetryException(Exception):
...
...
class Trade():
def __init__(self):
...
...
def some_other(self, id):
...
...
For example I need to make a call to a django model inside the "some_other(self, id)".
What is the best way of organizing this custom classes for using in a Django view?
Best Regards,
There is no difference between using a python class in a django view and using a class inside a "normal" python function.
Instantiate your class, and call its methods.
Do you have a Trade model? If so, would it makes sense to put that functionality in the Trade model class?
If you need to call something inside of your Trade class, what you are calling has to be in scope. If you are querying a model you can import it in the module Trade is defined in, and you can access it as you expect.
from yourproject.yourapp.models import AModel
class Trade(object):
def some_other(self, id):
return AModel.objects.filter(pk=id)

Using Django's class based views to create a list of multiple objects

What's the correct way to extend django's class based views to display a page with a series of list objects.
Does it make sense to create a ListView class for one of the objects and then pass the data for the other object lists in the get_context function?
What's the 'right' way to do this?
I'd just write a custom class view that inherits from the TemplateView and put your logic in the get_context_data method.