Dynamic Flatpage per Model instance - django

I was wondering what would be the best way to create dynamic report documents (html) with Django. The data used for every report is stored in model instances which then gets passed to a html template which renders the report. The user should be able to edit all the rendered content via a front-end editor.
Is it possible to use flat pages to store a complete document in html per report instance? Looking at the documentation it seems as if the flatpages app is mainly used for global static pages which can be edited via admintools. Is it possible to set up one Flatpage instance for each report instance in the system (e.g. Model Report with a Flatpage foreign key)?

take a look at the flatpage model. It has a textfield for its content.
https://github.com/django/django/blob/master/django/contrib/flatpages/models.py
In your code, you can store any content and store it in the textfield.
( I will store it as json string )
Then in your template, you can parse that string into json and use it.
Depending on your front end editor, you should can always parse the data into a json and store it back into flatpage model using via a post request and handled with a custom view with logics to convert your return json data and store it in the content field in your flatpage model. ( You will need to use json.dumps to convert your json object into string )
Let me know if you need a more specific example.

Related

Generating dynamic list columns fields in SharePoint list form

We have created a custom list form with SharePoint designer and now the requirement is like below:
User will request server creation using this form and now server can be of any type which user can choose from a drop-down such as Production,stage,Test or multiple production servers are required. And for each server type, there will be corresponding 20-25 fields which user need to fill for that server details. so i want to know the best way to achieve this as we cant create 200-250 list columns in this list and scrolling also will be a difficult task while user will submit the request. So what is the best way to achieve this requirement?
You can create a list containing all server types that will be used to create a server type lookup. Then you can create a list with a 'Server Type' column and 'Server Requirements' (multiple lines of text) column. You can store all requirements for a particular server as a JSON object e.g.:
{"RAM":"8GB", "CPU":"4"}
OR you can create a nested JSON object for each server type e.g.
{"ServerType": "Staging", "Requirements": {"RAM":"8GB", "CPU":"4"}}
wherever you want to show/send/populate data, you just need to retrieve this json and parse.
Hope this helps.
first I think the recommended way would be to try OOTB SharePoint solutions and in Your case I think You could try to use ContentTypes and OOTB list forms.
Lets say You create a content type per server type. To each type You add only the 20-25 fields that are corresponding to this server type. Then in list settings in advanced settings You turn on content type management and You add does content types to the list (also hide the default Element content type). After that when the user will want to add a new item to the list he will be albo to chose between the content types (server types in Your case), and after that the form will have only the fields that are added to this content type. Also in edit form the user will be able to pick between content types and will see only the corresponding fields. Please see the attached screens to also understand what I mean on a very simple case:
list
ContentType1 (ColumnA)
ContentType2 (ColumnB, ColumnC)
if this OOTB features are not enough and You already created a custom form using SharePoint Designer
Then You should be able to attach custom javascript file to it and jQuery library. The javascript You may store under _layouts path on server. Every field in the form has its own tr (row) You could to each row attach some custom css class like- class="forServerProd allFields" and then in js You could listen onChange event of Your list and show or hide fields with $(".allFields").css('dispaly','none'); // first hide all
$(".forServerProd ").css('dispaly','table-row'); // then show only relevant

How can I fetch data from a website to my local Django Website?

I am rather new to Django and I need to fetch some data from a website. For example I want the top ten posts of the day from Reddit. I know of a "request" module for the same.But I am not sure where and how should I implement it and will it be important to store the data in a model or not.
You can create a helper class named like network.py and implement functions to fetch the data.
If you want to store them in the database you can create appropriate models otherwise you can directly import and call the function and use the data returned from network.py in your view.

Accessing url of ImageField via values() method on django queryset

I have a data model in Django where I save photos uploaded by users. There's an attribute called image_file in the Photo model (of the type ImageField) where - among other things - the image's url is stored. I can successfully access this url with item.image_file.url in the template (and view) for instance.
However, I can't seem to be able to do the following in the view:
Photo.objects.filter(owner=user).order_by('-id').values('id','image_file.url')[:10]
I.e. For a particular user, I'm trying to get the 10 latest photo objects' image urls along with object ids. This gives me FieldError: Cannot resolve keyword 'image_file.url' into field. Shouldn't this have worked?
I understand I can retrieve the entire object and do the filtering in the template, but I felt it's more optimal to solely retrieve the fields I actually need instead of the full object.
p.s. it's a postgresql backend and the underlying Storage class has been over-ridden
The url is a property, not a value in the database (FileField code) which is why you can't get to it from values(). As far as I can see, you'd have to get the url value separately...
You might want to take a look at only() though. If you go that route, you should probably watch the SQL queries with something like Django Debug Toolbar. If the url property tries to retrieve a field that wasn't included in only(), it will likely make a separate SQL call.

User controlled presentation of data in widgets on a dashboard app - Best Practices?

Consider a very simple dashboard application in Django. It has 2 models:
Page
Widget
Naturally, Page and Widget have a ManyToMany relationship.
Like any good dashboard implementation, the designers can change 3 things in a widget:
Data source that drives the widget
Placement of widget on the Page
Presentation of Data inside a widget
The Data is specified using a URL field in the Widget and is being served by a REST API based on Django REST Framework with the django-filter backend.
The Placement on the Page is catered using the excellent Gridster.
This leaves the Presentation part. I have two possible solutions:
Attach a template TextField with the Widget. Data will be fetched from web services in JSON format and rendered according to the template (handlebars) defined in Widget on the client side.
Pass the template name as query string in the URL to the REST API and render the Data using the user-specified template.
Now that the context is clearly defined (hopefully), following are my questions:
Is there any way I can choose the first solution and still be able to use the automatic forms generated by the DRF Serializers?
If not, and I choose the second solution, are there any potential pit-falls regarding security, code maintenance, code quality, testing and the like? Why have I not seen anyone else doing this i.e. letting the user select the template via query string?
Is there any other solution that I am missing?
Your first options seems most promising: fetch the data as JSON and insert it into templates on the client. All good.
So can you do that "and still be able to use the automatic forms generated by the DRF Serializers"? — Short answer, it depends what you mean by "automatic forms".
Serializers take a data dictionary, validate it and (for ModelSerializer subclasses) convert it into a (model) object instance for you. If by "automatic forms" you mean will you still be able to this validation behaviour, then the answer is yes. Create your JSON payload on the client and send an appropriate HTTP request to the API. Django Rest Framework's Serializers will work as expected.
If (though) by "automatic forms" you mean will you still be able to use the HTML forms that DRF provides in its web broweasble API, then the answer is no. The browseable API is built around an HTML renderer returning entire web pages. These include a pretty-printed representation of the JSON you'll be using as well as the web-forms that, on this assumption, you're interested in.
If you go this route you'll need to generate the forms on the client, using whatever model, view, template and binding features your chosen library (libraries?) offer(s).
I hope that helps. Good luck.

Hidden select multiple items Django

I've got several forms in my django app that require support for attachments. Each form instance may have any number of attachments, including none. I want to present a jQuery based upload widget for managing these uploads, allowing the uploads to be processed asynchronously. The attachments are stored in their own model, so there is then a many-to-many from the attachments model to each model that requires attachments. When an attachment is sucessfully uploaded and processed, the view handling the upload will return the id in the attachments model, which will then be inserted into a hidden field on the form. I'm currently trying to decide how best to represent this in the form.
One method would be to simply have a single hidden input which takes a comma separated list of ids. This would then require quite a lot of manual processing and validation on submission, which I can't help feeling could be avoided.
Elsewhere, I've used a HiddenInput for a single value where I'm doing something similar and dynamically adding items to the related model in the form. I can't however see how I can extend this directly to a Many to Many from a simple Foreign Key.
Anyone able to suggest the best way to go about doing this?
Try to use formsets or model_formsets to create a form for creating/editing multiple objects, also you can use javascript to add forms dynamically in your browser.