I'm currently trying to create a custom ModelView for a detail_view using a template on Flask Admin. However I'm struggling to figure out how to access the instance of a model that the user is viewing the details of.
This is my custom ModelView:
class ShopModelView(MyModelView):
can_view_details = True
details_template = "custom_detail_view.html"
#expose('/details/', methods=('GET', 'POST'))
def details_view(self):
self._template_args['all_transactions'] = #current_shop_object#.transactions.order_by(Transaction.timestamp.desc())
return super(ShopModelView, self).details_view()
Looking at this post, the class of the model can be obtained using self.model, however this returns the class rather than the instance of the specific model being accessed.
The documentation on Templates and ModelView doesn't seem to explain it.
How can I get the instance of the specific model being accessed?
Turns out that the current instance of the model can be accessed via the variable model inside the jinja2 template file. So instead of parsing the variable as a template argument like I was trying to: self._template_args['all_transactions'] = #current_shop_object#.transactions.order_by(Transaction.timestamp.desc()),
{{ model.transactions.all() }} achieves the result I was aiming for.
Related
I want to overwrite create.html and edit.html used for models derived from Wagtails 'PageModel'.
If I understand the docs correctly it should be as simple as specifying the attributes:
class MyAdmin(ModelAdmin):
model = MyPage
create_template_name = "myapp/create.html"
edit_template_name = "myapp/edit.html"
My templates are located at projectroot/templates/myapp. It works fine if my model is a Django model but for a PageModel based model the create view still uses wagtailadmin/pages/create.html. I also tried the other location patterns mentioned in the docs w/o success.
Is it possible to change the edit and create templates for a PageModel? Or do the same limitations as for views apply, i.e. only index.html and inspect.html can be overwritten?
ModelAdmin does not provide create, edit, or delete functionality for Page models, as per the documentation note.
NOTE: modeladmin only provides ‘create’, ‘edit’ and ‘delete’ functionality for non page type models (i.e. models that do not extend wagtailcore.models.Page). If your model is a ‘page type’ model, customising any of the following will not have any effect.
It can be a bit confusing as the ModelAdmin system would seem to work for page models also, but there are some other ways to modify how your page can be edited. These will not be scoped to the ModelAdmin area though.
Option 1 - customise the generated form for your MyPage model
If you only want to customise how the edit page form that gets generated you can modify the base_form_class on your page model.
Wagtail has documentation about how to create a custom page form.
Note: WagtailAdminPageForm extends Django's ModelFormMetaClass
Example
from django import forms
from django.db import models
from wagtail.admin.forms import WagtailAdminPageForm
from wagtail.core.models import Page
class EventPageForm(WagtailAdminPageForm):
# ...
class MyPage(Page):
# ...
base_form_class = MyPageForm
Option 2 - customise the view via hooks
To customise the create & edit views for the normal (e.g. clicking edit page on the Wagtail user bar or explorer) page editing interface, you will need to use Wagtail hooks. Here you have access to the request so you will likely be able to determine if you are in the ModelAdmin area.
Create a file called wagtail_hooks.py in your app folder and provide a hook that will return a custom response (this will need to be rendered by your custom view.).
There are separate hooks for before_create_page and before_edit_page
Example from before_create_page docs below.
from wagtail.core import hooks
from .models import AwesomePage
from .admin_views import edit_awesome_page
#hooks.register('before_create_page')
def before_create_page(request, parent_page, page_class):
# Use a custom create view for the AwesomePage model
if page_class == AwesomePage:
return create_awesome_page(request, parent_page)
```python
How would I be able to override and add some extra code when creating a new row in a table via the Admin panel in Flask?
For example: User enters info for a new row in the 'Post' table and clicks save. I want to add some code to automate a process with that new row information.
You can override the methods on_model_change to perform actions before saving/updating a new model, or after_model_change to do something after, obviously.
You can inherit from the class BaseModelView or ModelView if you are using Flask-SqlAchemy.
In every cases, 3 arguments are provided to play with : the form used by the view, the new/updated model and the flag is_created to know if the model is new (True) or updated.
You can defined the model view like below :
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
class PostView(ModelView):
def after_model_change(self, form, model, is_created):
print(form, model, is_created)
def on_model_change(self, form, model, is_created):
print(form, model, is_created)
admin = Admin(template_mode='bootstrap3')
admin.add_view(PostView(Post, db.session, name='Posts'))
Note : You have access to similar methods for the deleting part with on_model_delete and after_model_delete, except you only have the model given as argument.
For the life of me I cannot figure this out and I'm having trouble finding information on it.
I have a Django view which accepts an argument which is a primary key (e.g: URL/problem/12) and loads a page with information from the argument's model.
I want to mock models used by my view for testing but I cannot figure it out, this is what I've tried:
#patch('apps.problem.models.Problem',)
def test_search_response(self, problem, chgbk, dispute):
problem(problem_id=854, vendor_num=100, chgbk=122)
request = self.factory.get(reverse('dispute_landing:search'))
request.user = self.user
request.usertype = self.usertype
response = search(request, problem_num=12)
self.assertTemplateUsed('individual_chargeback_view.html')
However - I can never get the test to actually find the problem number, it's as if the model does not exist.
I think that's because if you mock the entire model itself, the model won't exist because any of the functions to create/save it will have been mocked. If Problem is just a mock model class that hasn't been modified in any way, it knows nothing about interacting with the database, the ORM, or anything that could be discoverable from within your search() method.
One approach you could take rather than mocking models themselves would be to create FactoryBoy model factories. Since the test database is destroyed with each test run, these factories are a great way to create test data:
http://factoryboy.readthedocs.io/en/latest/
You could spin up a ProblemFactory like so:
class ProblemFactory(factory.Factory):
class Meta:
model = Problem
problem_id = factory.Faker("pyint")
vendor_num = factory.Faker("pyint")
chgbk = factory.Faker("pyint")
Then use it to create a model that actually exists in your database:
def test_search_response(self, problem, chgbk, dispute):
problem = ProblemFactory(problem_id=854, vendor_num=100, chgbk=122)
request = self.factory.get(reverse('dispute_landing:search', kwargs={'problem_id':problem.id}))
request.user = self.user
request.usertype = self.usertype
response = search(request, problem_num=854)
self.assertTemplateUsed('individual_chargeback_view.html')
What's the right way to get the URL for a flask-admin ModelView?
Here's a very simple example:
my_admin_view.py
from flask.ext.admin.contrib.sqla import ModelView
from common.flask_app import app
from models import db, User, Role
admin = Admin(app, name="Boost Admin")
admin.add_view(ModelView(User, db.session, category="model"))
admin.add_view(ModelView(Role, db.session, category="model"))
my_admin_template.html
...
<p>Check out my user admin link:</p>
User view link
{# ______________
what argument to pass in here?? #}
...
What's the correct argument to pass to url_for(...)?
I've tried modelview.user, my_admin_view.modelview.user, etc. None of them seem to resolve correctly, and I'd like to avoid hardcoding the link.
thanks!
OK I figured it out after reading the source code for ModelView.
First, make sure that endpoints are named (it's possible to do it without named endpoints, but this makes the code much clearer):
from flask.ext.admin.contrib.sqla import ModelView
from models import db, User, Role
admin = Admin(app, name="Boost Admin")
admin.add_view(ModelView(User,db.session,category="model", endpoint="model_view_user"))
admin.add_view(ModelView(Role,db.session,category="model", endpoint="model_view_role"))
...now in the template, you can reference the basic model view as follows:
URL for User model default view is: {{model_view_user.index_view}}
URL for Role model default view is: {{model_view_role.index_view}}
The index_view function is defined here, and implements the default view for a flask admin ModelView.
See the section Generating URLs in the Flask-Admin introduction.
It says to "use the lowercase name of the model as the prefix". Add a dot, and the name of the view.
index_view for the overview list.
create_view for creating a new row.
edit_view for modifying an existing row.
So you can easily do:
url_for('user.index_view')
url_for('role.create_view')
url_for('user.edit_view', id=1)
It should be
url_for('admin.user')
If you read the flask-admin docs here, for generating URLs, it clearly says:
If you want to generate a URL for a particular view method from outside, the following rules apply:
....
3. For model-based views the rules differ - the model class name should be used if an endpoint name is not provided.
Basically what I'm trying to achieve is a multi-model django app where different models take advantage of the same views. For example I've got the models 'Car' 'Make' 'Model' etc and I want to build a single view to perform the same task for each, such as add, delete and edit, so I don't have to create a seperate view for add car, ass make etc. I've built a ModelForm and Model object for each and would want to create a blank object when adding and a pre-populated form object when editing (through the form instance arg), with objects being determined via url parameters.
Where I'm stuck is that I'm not sure what the best way to so this is. At the moment I'm using a load of if statements to return the desired object or form based on parameters I'm giving it, which get's a bit tricky when different forms need specifying and whether they need an instance or not. Although this seems to be far from the most efficient way of achieving this.
Django seems to have functions to cover most repetitive tasks, is there some magic I'm missing here?
edit - Here's an example of what I'm doing with the arguments I'm passing into the url:
def edit_object(request, object, id):
if(object==car):
form = carForm(instance = Car.objects.get(pk=id)
return render(request, 'template.html', {'form':form})
What about using Class Based Views? Using CBVs is the best way in Django to make reusable code. For this example maybe it can be a little longer than function based views, but when the project grows up it makes the difference. Also remember "Explicit is better than implicit".
urls.py
# Edit
url(r'^car/edit/(?P<pk>\d+)/$', EditCar.as_view(), name='edit-car'),
url(r'^make/edit/(?P<pk>\d+)/$', EditMake.as_view(), name='edit-make'),
# Delete
url(r'^car/delete/(?P<pk>\d+)/$', DeleteCar.as_view(), name='delete-car'),
url(r'^make/delete/(?P<pk>\d+)/$', DeleteMake.as_view(), name='delete-make'),
views.py
class EditSomethingMixin(object):
"""Use Mixins to reuse common behavior"""
template_name = 'template-edit.html'
class EditCar(EditSomethingMixin, UpdateView):
model = Car
form_class = CarForm
class EditMake(EditSomethingMixin, UpdateView):
model = Make
form_class = MakeForm
class DeleteSomethingMixin(object):
"""Use Mixins to reuse common behavior"""
template_name = 'template-delete.html'
class DeleteCar(DeleteSomethingMixin, DeleteView):
model = Car
class DeleteMake(DeleteSomethingMixin, DeleteView):
model = Make
Just pass your class and form as args to the method then call them in the code.
def edit_object(request, model_cls, model_form, id):
form = model_form(instance = model_cls.objects.get(pk=id)
return render(request, 'template.html', {'form':form})
then just pass in the correct classes and forms in your view methods
def edit_car(request,id):
return edit_object(request, Car, CarForm, id)
each method knows what classes to pass, so you eliminate the if statements.
urls.py
url(r'^car/delete/(?<pk>\d+)/', edit, {'model': Car})
url(r'^make/delete/(?<pk>\d+)/', edit, {'model': Make})
views.py
def edit(request, id, model):
model.objects.get(id=id).delete()