I would like to store json from request body directly into mongo collection without creation of specific model in django. I cannot create it because specific fields in json may change and new ones can be introduced, so I would like rather to store it as it is without this abstraction layer. Is it possible?
Related
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.
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.
I am currently using Tastypie to provide a programmatic interface to my Django database. On problem that I've run into a couple of times is that when client code uploads data for a field that doesn't exist, Tastypie ignores it. This means that the client code has no idea that some of the data that it tried to upload was ignored. I'd like to tell the client that it tried to upload an unknown field, possible with a status code 406 (not acceptable).
I have two related questions:
Is it appropriate for RESTful design to reject this extra data?
If so, is there a tidy way to do this through Tastypie?
As an example of my concern, consider this toy Tastypie API:
from tastypie import resources, fields
class DemoResource(resources.ModelResource):
name = fields.CharField()
optional = fields.CharField(blank=True)
If client code uploaded the json data: {name: "new data", optioanl: "this field is misspelled"}, the misspelled optional field would be ignored. My current plan is to use a Tastypie validator to compare the bundle data against the bundle object, but this seems really non-DRY.
is there a good package to parse XML/JSON data and map it to Django model's object fields? The ideal solution would be the one that supports object updates. What can you recommend?
UPDATE:
Will tell a bit about my requirements. There affiliate programs that supply partners with content via XML feeds. What I need is to parse the feed time from time and get content out of it and update already existing content (objects).
I'd like to take advantage of all the field validation that comes with ModelForm and the convenience of handling related objects that comes with inlineformset_factory however I'd like to be able to post JSON data instead of using forms on my site.
In the past I've been using hidden forms and then serializing the form data and sending that to the server for AJAX like communication.
I'd prefer to do this in a less hacky way - ie. somehow co-opt forms to accept JSON data in a nice way.
Any suggestions/ examples?
Try loading the data via:
import json
data_dict = json.loads(data_json)
and then creating a new instance of the factory via
YourFactory(**data_dict)